/** * Creates a new processor object for use in the tests. */ protected function setUp() { parent::setUp(); $this->processor = new RoleFilter(array(), 'role_filter', array()); /** @var \Drupal\search_api\IndexInterface $index */ $index = $this->getMock('Drupal\\search_api\\IndexInterface'); $node_datasource = $this->getMock('Drupal\\search_api\\Datasource\\DatasourceInterface'); $node_datasource->expects($this->any())->method('getEntityTypeId')->will($this->returnValue('node')); /** @var \Drupal\search_api\Datasource\DatasourceInterface $node_datasource */ $user_datasource = $this->getMock('Drupal\\search_api\\Datasource\\DatasourceInterface'); $user_datasource->expects($this->any())->method('getEntityTypeId')->will($this->returnValue('user')); /** @var \Drupal\search_api\Datasource\DatasourceInterface $user_datasource */ $item = Utility::createItem($index, Utility::createCombinedId('entity:node', '1:en'), $node_datasource); $node = $this->getMockBuilder('Drupal\\Tests\\search_api\\TestNodeInterface')->disableOriginalConstructor()->getMock(); /** @var \Drupal\node\NodeInterface $node */ $item->setOriginalObject(EntityAdapter::createFromEntity($node)); $this->items[$item->getId()] = $item; $item = Utility::createItem($index, Utility::createCombinedId('entity:user', '1:en'), $user_datasource); $account1 = $this->getMockBuilder('Drupal\\Tests\\search_api\\TestUserInterface')->disableOriginalConstructor()->getMock(); $account1->expects($this->any())->method('getRoles')->will($this->returnValue(array('authenticated' => 'authenticated', 'editor' => 'editor'))); /** @var \Drupal\user\UserInterface $account1 */ $item->setOriginalObject(EntityAdapter::createFromEntity($account1)); $this->items[$item->getId()] = $item; $item = Utility::createItem($index, Utility::createCombinedId('entity:user', '2:en'), $user_datasource); $account2 = $this->getMockBuilder('Drupal\\Tests\\search_api\\TestUserInterface')->disableOriginalConstructor()->getMock(); $account2->expects($this->any())->method('getRoles')->will($this->returnValue(array('authenticated' => 'authenticated'))); /** @var \Drupal\user\UserInterface $account2 */ $item->setOriginalObject(EntityAdapter::createFromEntity($account2)); $this->items[$item->getId()] = $item; }
/** * @covers ::onPageContext */ public function testOnPageContext() { $account = $this->prophesize(AccountInterface::class); $account->id()->willReturn(1); $user = $this->prophesize(UserInterface::class); $data_definition = new DataDefinition(['type' => 'entity:user']); $this->typedDataManager->create($data_definition, $user)->willReturn(EntityAdapter::createFromEntity($user->reveal())); $this->typedDataManager->getDefaultConstraints($data_definition)->willReturn([]); $this->typedDataManager->createDataDefinition('entity:user')->will(function () use($data_definition) { return $data_definition; }); $this->executable->expects($this->once())->method('addContext')->with('current_user', $this->isInstanceOf(Context::class)); $user_storage = $this->prophesize(EntityStorageInterface::class); $user_storage->load(1)->willReturn($user->reveal()); $entity_manager = $this->prophesize(EntityManagerInterface::class); $entity_manager->getStorage('user')->willReturn($user_storage->reveal()); $route_param_context = new CurrentUserContext($account->reveal(), $entity_manager->reveal()); $route_param_context->onPageContext($this->event); }
/** * Tests whether indexed items are correctly preprocessed. */ public function testProcessIndexItems() { /** @var \Drupal\node\Entity\Node $node */ $node = $this->getMockBuilder('Drupal\\node\\Entity\\Node')->disableOriginalConstructor()->getMock(); $body_value = array('Some text value'); $fields = array('search_api_url' => array('type' => 'string'), 'entity:node/body' => array('type' => 'text', 'values' => $body_value)); $items = $this->createItems($this->index, 2, $fields, EntityAdapter::createFromEntity($node)); // Process the items. $this->processor->preprocessIndexItems($items); // Check the valid item. $field = $items[$this->itemIds[0]]->getField('search_api_url'); $this->assertEquals(array('http://www.example.com/node/example'), $field->getValues(), 'Valid URL added as value to the field.'); // Check that no other fields were changed. $field = $items[$this->itemIds[0]]->getField('body'); $this->assertEquals($body_value, $field->getValues(), 'Body field was not changed.'); // Check the second item to be sure that all are processed. $field = $items[$this->itemIds[1]]->getField('search_api_url'); $this->assertEquals(array('http://www.example.com/node/example'), $field->getValues(), 'Valid URL added as value to the field in the second item.'); }
/** * @covers ::onPageContext */ public function testOnPageContext() { $account = $this->prophesize(AccountInterface::class); $account->id()->willReturn(1); $user = $this->prophesize(UserInterface::class); $data_definition = new DataDefinition(['type' => 'entity:user']); $this->typedDataManager->create($data_definition, $user)->willReturn(EntityAdapter::createFromEntity($user->reveal())); $this->typedDataManager->getDefaultConstraints($data_definition)->willReturn([]); $this->typedDataManager->createDataDefinition('entity:user')->will(function () use($data_definition) { return $data_definition; }); $this->page->addContext('current_user', Argument::type(Context::class))->shouldBeCalled(); $user_storage = $this->prophesize(EntityStorageInterface::class); $user_storage->load(1)->willReturn($user->reveal()); $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class); $entity_type_manager->getStorage('user')->willReturn($user_storage->reveal()); $route_param_context = new CurrentUserContext($account->reveal(), $entity_type_manager->reveal()); $route_param_context->onPageContext($this->event); }
/** * Creates a new processor object for use in the tests. */ protected function setUp() { parent::setUp(); $this->processor = new NodeStatus(array(), 'node_status', array()); $datasource = $this->getMock('Drupal\\search_api\\Datasource\\DatasourceInterface'); $datasource->expects($this->any())->method('getEntityTypeId')->will($this->returnValue('node')); /** @var \Drupal\search_api\Datasource\DatasourceInterface $datasource */ $index = $this->getMock('Drupal\\search_api\\IndexInterface'); $index->expects($this->any())->method('getDatasources')->will($this->returnValue(array($datasource))); /** @var \Drupal\search_api\IndexInterface $index */ $item = Utility::createItem($index, Utility::createCombinedId('entity:node', '1:en'), $datasource); $unpublished_node = $this->getMockBuilder('Drupal\\Tests\\search_api\\TestNodeInterface')->disableOriginalConstructor()->getMock(); $unpublished_node->expects($this->any())->method('isPublished')->will($this->returnValue(FALSE)); /** @var \Drupal\node\NodeInterface $unpublished_node */ $item->setOriginalObject(EntityAdapter::createFromEntity($unpublished_node)); $this->items[$item->getId()] = $item; $item = Utility::createItem($index, Utility::createCombinedId('entity:node', '2:en'), $datasource); $published_node = $this->getMockBuilder('Drupal\\Tests\\search_api\\TestNodeInterface')->disableOriginalConstructor()->getMock(); $published_node->expects($this->any())->method('isPublished')->will($this->returnValue(TRUE)); /** @var \Drupal\node\NodeInterface $published_node */ $item->setOriginalObject(EntityAdapter::createFromEntity($published_node)); $this->items[$item->getId()] = $item; }
/** * {@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); }
/** * Tests whether the "Item language" field is properly added to indexed items. * * @see \Drupal\search_api\Plugin\search_api\processor\Language::preprocessIndexItems() */ public function testPreprocessIndexItems() { $fields = array('search_api_language' => array('type' => 'string')); $items = $this->createItems($this->index, 3, $fields); $object1 = $this->getMock('Drupal\\Tests\\search_api\\TestContentEntityInterface'); $object1->expects($this->any())->method('language')->will($this->returnValue(new CoreLanguage(array('id' => 'en')))); /** @var \Drupal\Core\Entity\ContentEntityInterface $object1 */ $items[$this->itemIds[0]]->setOriginalObject(EntityAdapter::createFromEntity($object1)); $object2 = $this->getMock('Drupal\\Tests\\search_api\\TestContentEntityInterface'); $object2->expects($this->any())->method('language')->will($this->returnValue(new CoreLanguage(array('id' => 'es')))); /** @var \Drupal\Core\Entity\ContentEntityInterface $object2 */ $items[$this->itemIds[1]]->setOriginalObject(EntityAdapter::createFromEntity($object2)); $object3 = $this->getMock('Drupal\\Tests\\search_api\\TestComplexDataInterface'); /** @var \Drupal\Tests\search_api\TestComplexDataInterface $object3 */ $items[$this->itemIds[2]]->setOriginalObject($object3); $this->processor->preprocessIndexItems($items); $this->assertEquals(array('en'), $items[$this->itemIds[0]]->getField('search_api_language')->getValues(), 'The "Item language" value was correctly set for an English item.'); $this->assertEquals(array('es'), $items[$this->itemIds[1]]->getField('search_api_language')->getValues(), 'The "Item language" value was correctly set for a Spanish item.'); $this->assertEquals(array(CoreLanguage::LANGCODE_NOT_SPECIFIED), $items[$this->itemIds[2]]->getField('search_api_language')->getValues(), 'The "Item language" value was correctly set for a non-translatable item.'); }