/** * 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 ::getIterator */ public function testGetIterator() { $iterator = $this->entityAdapter->getIterator(); $fields = iterator_to_array($iterator); $this->assertArrayHasKey('id', $fields); $this->assertArrayHasKey('revision_id', $fields); $this->assertEquals(count($fields), 2); $this->entityAdapter->setValue(NULL); $this->assertEquals(new \ArrayIterator([]), $this->entityAdapter->getIterator()); }
/** * @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; }
/** * 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.'); }