/** * @param bool $withRelations * * @dataProvider testDataProvider */ public function testBuildFormRegularGuesser($withRelations) { $entityName = 'Test\\Entity'; $this->doctrineHelperMock->expects($this->once())->method('getEntityIdentifierFieldNames')->with($entityName)->willReturn(['id']); $fields = [['name' => 'oneField', 'type' => 'string', 'label' => 'One field'], ['name' => 'anotherField', 'type' => 'string', 'label' => 'Another field']]; if ($withRelations) { $fields[] = ['name' => 'relField', 'relation_type' => 'ref-one', 'label' => 'Many to One field']; } $this->entityFieldMock->expects($this->once())->method('getFields')->willReturn($fields); $this->formConfigMock->expects($this->at(0))->method('getConfig')->with($entityName, 'oneField')->willReturn(new Config(new FieldConfigId('form', $entityName, 'someField'), ['is_enabled' => false])); $this->formConfigMock->expects($this->at(1))->method('getConfig')->with($entityName, 'anotherField')->willReturn(new Config(new FieldConfigId('form', $entityName, 'anotherField'), ['is_enabled' => true])); if ($withRelations) { $this->formConfigMock->expects($this->at(2))->method('getConfig')->with($entityName, 'relField')->willReturn(new Config(new FieldConfigId('form', $entityName, 'relField'), ['is_enabled' => true])); $this->translatorMock->expects($this->at(0))->method('trans')->with('oro.entity.form.entity_fields')->willReturn('Fields'); $this->translatorMock->expects($this->at(1))->method('trans')->with('oro.entity.form.entity_related')->willReturn('Relations'); } $form = $this->factory->create($this->type, null, ['entity' => $entityName, 'with_relations' => $withRelations]); $view = $form->createView(); $this->assertEquals('update_field_choice', $view->vars['full_name'], 'Failed asserting that field name is correct'); $this->assertNotEmpty($view->vars['configs']['component']); $this->assertEquals('entity-field-choice', $view->vars['configs']['component']); $this->assertEquals('update_field_choice', $form->getConfig()->getType()->getName(), 'Failed asserting that correct underlying type was used'); if ($withRelations) { $this->assertCount(2, $view->vars['choices'], 'Failed asserting that choices are grouped'); } else { $this->assertCount(1, $view->vars['choices'], 'Failed asserting that choices exists'); /** @var ChoiceView $choice */ $choice = reset($view->vars['choices']); $this->assertEquals('Another field', $choice->label); } }
/** * @param array $fields * @param array $configValues * @param array $expected * * @dataProvider fieldsDataProvider */ public function testGetFields(array $fields, array $configValues, array $expected) { $entity = new \StdClass(); foreach ($fields as $field) { /** @var ConfigInterface $field */ $fieldId = $field->getId(); /** @var FieldConfigId $fieldId */ $fieldName = $fieldId->getFieldName(); $entity->{$fieldName} = $fieldName; } $this->configProvider->expects($this->once())->method('filter')->will($this->returnValue($fields)); $config = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface'); $this->configProvider->expects($this->any())->method('getConfigById')->will($this->returnValue($config)); foreach ($configValues as $key => $configValue) { $config->expects($this->at($key))->method('get')->will($this->returnCallback(function ($value, $strict, $default) use($configValue) { if (!is_null($configValue)) { return $configValue; } return $default; })); } $this->dispatcher->expects($this->exactly(sizeof($fields)))->method('dispatch'); $rows = $this->extension->getFields($entity); $this->assertEquals(json_encode($expected), json_encode($rows)); }
public function testWarmUpCacheFilterConfigsByScope() { $config1 = new Config(new EntityConfigId('ownership', 'AcmeBundle\\Entity\\User')); $config2 = new Config(new EntityConfigId('ownership', 'AcmeBundle\\Entity\\Account')); $this->configProvider->expects($this->once())->method('getConfigs')->willReturn([$config1, $config2]); $this->securityConfigProvider->expects($this->atLeastOnce())->method('hasConfig')->willReturn(true); $securityConfig1 = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface'); $securityConfig1->expects($this->once())->method('get')->with('group_name')->willReturn(''); $securityConfig2 = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface'); $securityConfig2->expects($this->once())->method('get')->with('group_name')->willReturn('commerce'); $this->securityConfigProvider->expects($this->atLeastOnce())->method('getConfig')->will($this->onConsecutiveCalls($securityConfig1, $securityConfig2)); $this->cache->expects($this->once())->method('fetch')->with($this->equalTo('AcmeBundle\\Entity\\Account')); $this->provider->warmUpCache(); }
public function testHandleUpdateNotAllowed() { $massActionMock = $this->getMock(MassActionInterface::class); $datagridMock = $this->getMock('Oro\\Bundle\\DataGridBundle\\Datagrid\\DatagridInterface'); $iteratorMock = $this->getMock('Oro\\Bundle\\DataGridBundle\\Datasource\\Orm\\IterableResultInterface'); $entityName = 'Test\\EntityName'; $hasConfig = true; $isEnabled = true; $isGranted = false; $data = []; $this->actionRepoMock->expects($this->once())->method('getEntityName')->with($datagridMock)->will($this->returnValue($entityName)); $this->configMock->expects($this->once())->method('hasConfig')->with($entityName)->will($this->returnValue($hasConfig)); $this->configMock->expects($this->once())->method('getConfig')->with($entityName)->will($this->returnValue(new Config(new EntityConfigId('extend', $entityName), ['update_mass_action_enabled' => $isEnabled]))); $this->securityMock->expects($this->once())->method('isGranted')->with('EDIT', 'entity:' . $entityName)->will($this->returnValue($isGranted)); $this->actionRepoMock->expects($this->never())->method('batchUpdate'); $this->transMock->expects($this->once())->method('trans')->will($this->returnValue(uniqid())); $options = ActionConfiguration::create(['success_message' => '', 'error_message' => '']); $massActionMock->expects($this->any())->method('getOptions')->will($this->returnValue($options)); $this->transMock->expects($this->once())->method('trans')->with('', ['%error%' => 'Action not configured or not allowed']); $this->loggerMock->expects($this->once())->method('debug'); $actionResponse = $this->handler->handle(new MassActionHandlerArgs($massActionMock, $datagridMock, $iteratorMock, $data)); $this->assertFalse($actionResponse->isSuccessful()); }
public function testSetRolePrivileges() { $role = new AccountUserRole('TEST'); $roleSecurityIdentity = new RoleSecurityIdentity($role); $firstClass = 'FirstClass'; $secondClass = 'SecondClass'; $unknownClass = 'UnknownClass'; $request = new Request(); $request->setMethod('GET'); $firstEntityPrivilege = $this->createPrivilege('entity', 'entity:' . $firstClass, 'VIEW'); $firstEntityConfig = $this->createClassConfigMock(true); $secondEntityPrivilege = $this->createPrivilege('entity', 'entity:' . $secondClass, 'VIEW'); $secondEntityConfig = $this->createClassConfigMock(false); $unknownEntityPrivilege = $this->createPrivilege('entity', 'entity:' . $unknownClass, 'VIEW'); $actionPrivilege = $this->createPrivilege('action', 'action', 'random_action'); $entityForm = $this->getMock('Symfony\\Component\\Form\\FormInterface'); $entityForm->expects($this->once())->method('setData')->willReturnCallback(function (ArrayCollection $actualPrivileges) use($firstEntityPrivilege) { $this->assertEquals([$firstEntityPrivilege], array_values($actualPrivileges->toArray())); }); $actionForm = $this->getMock('Symfony\\Component\\Form\\FormInterface'); $actionForm->expects($this->once())->method('setData')->willReturnCallback(function (ArrayCollection $actualPrivileges) use($actionPrivilege) { $this->assertEquals([$actionPrivilege], array_values($actualPrivileges->toArray())); }); $form = $this->getMock('Symfony\\Component\\Form\\FormInterface'); $form->expects($this->any())->method('get')->willReturnMap([['entity', $entityForm], ['action', $actionForm]]); $this->formFactory->expects($this->once())->method('create')->willReturn($form); $this->chainMetadataProvider->expects($this->once())->method('startProviderEmulation')->with(FrontendOwnershipMetadataProvider::ALIAS); $this->chainMetadataProvider->expects($this->once())->method('stopProviderEmulation'); $this->aclManager->expects($this->any())->method('getSid')->with($role)->willReturn($roleSecurityIdentity); $this->privilegeRepository->expects($this->any())->method('getPrivileges')->with($roleSecurityIdentity)->willReturn(new ArrayCollection([$firstEntityPrivilege, $secondEntityPrivilege, $unknownEntityPrivilege, $actionPrivilege])); $this->ownershipConfigProvider->expects($this->any())->method('hasConfig')->willReturnMap([[$firstClass, null, true], [$secondClass, null, true], [$unknownClass, null, false]]); $this->ownershipConfigProvider->expects($this->any())->method('getConfig')->willReturnMap([[$firstClass, null, $firstEntityConfig], [$secondClass, null, $secondEntityConfig]]); $this->handler->setRequest($request); $this->handler->createForm($role); $this->handler->process($role); }