Example #1
0
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setUp();
     $mock_data_definition = $this->getMock('Drupal\\Core\\TypedData\\DataDefinitionInterface');
     $this->contextDefinition = $this->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface')->setMethods(array('getDefaultValue', 'getDataDefinition'))->getMockForAbstractClass();
     $this->contextDefinition->expects($this->once())->method('getDefaultValue')->willReturn('test');
     $this->contextDefinition->expects($this->once())->method('getDataDefinition')->willReturn($mock_data_definition);
     $this->typedData = $this->getMock('Drupal\\Core\\TypedData\\TypedDataInterface');
     $this->typedDataManager = $this->getMockBuilder('Drupal\\Core\\TypedData\\TypedDataManager')->disableOriginalConstructor()->setMethods(array('create'))->getMock();
     $this->typedDataManager->expects($this->once())->method('create')->with($mock_data_definition, 'test')->willReturn($this->typedData);
 }
 /**
  * Tests required validation.
  *
  * @covers ::validate
  * @covers ::isValidationRequired
  * @covers ::setValidationRequired
  * @covers ::save
  * @covers ::preSave
  *
  * @expectedException \LogicException
  * @expectedExceptionMessage Entity validation was skipped.
  */
 public function testRequiredValidation()
 {
     $validator = $this->getMock('\\Symfony\\Component\\Validator\\ValidatorInterface');
     /** @var \Symfony\Component\Validator\ConstraintViolationList|\PHPUnit_Framework_MockObject_MockObject $empty_violation_list */
     $empty_violation_list = $this->getMockBuilder('\\Symfony\\Component\\Validator\\ConstraintViolationList')->setMethods(NULL)->getMock();
     $validator->expects($this->at(0))->method('validate')->with($this->entity->getTypedData())->will($this->returnValue($empty_violation_list));
     $this->typedDataManager->expects($this->any())->method('getValidator')->will($this->returnValue($validator));
     /** @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject $storage */
     $storage = $this->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
     $storage->expects($this->any())->method('save')->willReturnCallback(function (ContentEntityInterface $entity) use($storage) {
         $entity->preSave($storage);
     });
     $this->entityManager->expects($this->any())->method('getStorage')->with($this->entityTypeId)->will($this->returnValue($storage));
     // Check that entities can be saved normally when validation is not
     // required.
     $this->assertFalse($this->entity->isValidationRequired());
     $this->entity->save();
     // Make validation required and check that if the entity is validated, it
     // can be saved normally.
     $this->entity->setValidationRequired(TRUE);
     $this->assertTrue($this->entity->isValidationRequired());
     $this->entity->validate();
     $this->entity->save();
     // Check that the "validated" status is reset after saving the entity and
     // that trying to save a non-validated entity when validation is required
     // results in an exception.
     $this->assertTrue($this->entity->isValidationRequired());
     $this->entity->save();
 }
Example #3
0
 /**
  * Tests the clearCachedFieldDefinitions() method.
  *
  * @covers ::clearCachedFieldDefinitions()
  */
 public function testClearCachedFieldDefinitions()
 {
     $this->setUpEntityManager();
     $this->cache->expects($this->once())->method('deleteTags')->with(array('entity_field_info' => TRUE));
     $this->typedDataManager->expects($this->once())->method('clearCachedDefinitions');
     $this->entityManager->clearCachedFieldDefinitions();
 }
Example #4
0
 /**
  * Set up mocks for the getDefaultValue() method call.
  *
  * @param mixed $default_value
  *   The default value to assign to the mock context definition.
  */
 protected function setUpDefaultValue($default_value = NULL)
 {
     $mock_data_definition = $this->getMock('Drupal\\Core\\TypedData\\DataDefinitionInterface');
     $this->contextDefinition = $this->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface')->setMethods(array('getDefaultValue', 'getDataDefinition'))->getMockForAbstractClass();
     $this->contextDefinition->expects($this->once())->method('getDefaultValue')->willReturn($default_value);
     $this->contextDefinition->expects($this->once())->method('getDataDefinition')->willReturn($mock_data_definition);
     $this->typedData = $this->getMock('Drupal\\Core\\TypedData\\TypedDataInterface');
     $this->typedDataManager->expects($this->once())->method('create')->with($mock_data_definition, $default_value)->willReturn($this->typedData);
 }
 /**
  * @covers ::validate
  */
 public function testValidate()
 {
     $validator = $this->getMock('\\Symfony\\Component\\Validator\\ValidatorInterface');
     /** @var \Symfony\Component\Validator\ConstraintViolationList|\PHPUnit_Framework_MockObject_MockObject $empty_violation_list */
     $empty_violation_list = $this->getMockBuilder('\\Symfony\\Component\\Validator\\ConstraintViolationList')->setMethods(NULL)->getMock();
     $non_empty_violation_list = clone $empty_violation_list;
     $violation = $this->getMock('\\Symfony\\Component\\Validator\\ConstraintViolationInterface');
     $non_empty_violation_list->add($violation);
     $validator->expects($this->at(0))->method('validate')->with($this->entity->getTypedData())->will($this->returnValue($empty_violation_list));
     $validator->expects($this->at(1))->method('validate')->with($this->entity->getTypedData())->will($this->returnValue($non_empty_violation_list));
     $this->typedDataManager->expects($this->exactly(2))->method('getValidator')->will($this->returnValue($validator));
     $this->assertSame(0, count($this->entity->validate()));
     $this->assertSame(1, count($this->entity->validate()));
 }
 /**
  * @covers ::filterPluginDefinitionsByContexts
  *
  * @dataProvider providerTestFilterPluginDefinitionsByContexts
  */
 public function testFilterPluginDefinitionsByContexts($has_context, $definitions, $expected, $typed_data_definition = NULL)
 {
     if ($has_context) {
         $context = $this->getMock('Drupal\\Core\\Plugin\\Context\\ContextInterface');
         $expected_context_definition = (new ContextDefinition('expected_data_type'))->setConstraints(array('expected_constraint_name' => 'expected_constraint_value'));
         $context->expects($this->atLeastOnce())->method('getContextDefinition')->will($this->returnValue($expected_context_definition));
         $contexts = array($context);
     } else {
         $contexts = array();
     }
     if ($typed_data_definition) {
         $this->typedDataManager->expects($this->atLeastOnce())->method('getDefinition')->will($this->returnValueMap($typed_data_definition));
     }
     $this->assertSame($expected, $this->contextHandler->filterPluginDefinitionsByContexts($contexts, $definitions));
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     $this->id = 1;
     $values = array('id' => $this->id, 'uuid' => '3bb9ee60-bea5-4622-b89b-a63319d10b3a', 'defaultLangcode' => array(LanguageInterface::LANGCODE_DEFAULT => 'en'));
     $this->entityTypeId = $this->randomMachineName();
     $this->bundle = $this->randomMachineName();
     $this->entityType = $this->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
     $this->entityType->expects($this->any())->method('getKeys')->will($this->returnValue(array('id' => 'id', 'uuid' => 'uuid')));
     $this->entityManager = $this->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface');
     $this->entityManager->expects($this->any())->method('getDefinition')->with($this->entityTypeId)->will($this->returnValue($this->entityType));
     $this->uuid = $this->getMock('\\Drupal\\Component\\Uuid\\UuidInterface');
     $this->typedDataManager = $this->getMockBuilder('\\Drupal\\Core\\TypedData\\TypedDataManager')->disableOriginalConstructor()->getMock();
     $this->typedDataManager->expects($this->any())->method('getDefinition')->with('entity')->will($this->returnValue(['class' => '\\Drupal\\Core\\Entity\\Plugin\\DataType\\EntityAdapter']));
     $this->typedDataManager->expects($this->any())->method('getDefaultConstraints')->willReturn([]);
     $validation_constraint_manager = $this->getMockBuilder('\\Drupal\\Core\\Validation\\ConstraintManager')->disableOriginalConstructor()->getMock();
     $validation_constraint_manager->expects($this->any())->method('create')->willReturn([]);
     $this->typedDataManager->expects($this->any())->method('getValidationConstraintManager')->willReturn($validation_constraint_manager);
     $not_specified = new Language(array('id' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'locked' => TRUE));
     $this->languageManager = $this->getMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
     $this->languageManager->expects($this->any())->method('getLanguages')->will($this->returnValue(array(LanguageInterface::LANGCODE_NOT_SPECIFIED => $not_specified)));
     $this->languageManager->expects($this->any())->method('getLanguage')->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)->will($this->returnValue($not_specified));
     $this->fieldTypePluginManager = $this->getMockBuilder('\\Drupal\\Core\\Field\\FieldTypePluginManager')->disableOriginalConstructor()->getMock();
     $this->fieldTypePluginManager->expects($this->any())->method('getDefaultStorageSettings')->will($this->returnValue(array()));
     $this->fieldTypePluginManager->expects($this->any())->method('getDefaultFieldSettings')->will($this->returnValue(array()));
     $this->fieldItemList = $this->getMock('\\Drupal\\Core\\Field\\FieldItemListInterface');
     $this->fieldTypePluginManager->expects($this->any())->method('createFieldItemList')->willReturn($this->fieldItemList);
     $container = new ContainerBuilder();
     $container->set('entity.manager', $this->entityManager);
     $container->set('uuid', $this->uuid);
     $container->set('typed_data_manager', $this->typedDataManager);
     $container->set('language_manager', $this->languageManager);
     $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
     \Drupal::setContainer($container);
     $this->fieldDefinitions = array('id' => BaseFieldDefinition::create('integer'), 'revision_id' => BaseFieldDefinition::create('integer'));
     $this->entityManager->expects($this->any())->method('getFieldDefinitions')->with($this->entityTypeId, $this->bundle)->will($this->returnValue($this->fieldDefinitions));
     $this->entity = $this->getMockForAbstractClass('\\Drupal\\Core\\Entity\\ContentEntityBase', array($values, $this->entityTypeId, $this->bundle));
     $this->entityAdapter = EntityAdapter::createFromEntity($this->entity);
 }