diff --git a/tests/src/Kernel/Processor/RenderedItemTest.php b/tests/src/Kernel/Processor/RenderedItemTest.php index e156e84a..1ea60728 100644 --- a/tests/src/Kernel/Processor/RenderedItemTest.php +++ b/tests/src/Kernel/Processor/RenderedItemTest.php @@ -2,15 +2,21 @@ namespace Drupal\Tests\search_api\Kernel\Processor; +use Drupal\comment\CommentInterface; +use Drupal\comment\Entity\Comment; +use Drupal\comment\Entity\CommentType; use Drupal\Core\Entity\Entity\EntityViewMode; use Drupal\Core\TypedData\DataDefinitionInterface; -use Drupal\Core\TypedData\DataDefinition; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; +use Drupal\node\NodeInterface; use Drupal\search_api\Entity\Index; +use Drupal\search_api\Item\ItemInterface; use Drupal\search_api\Utility\Utility; -use Drupal\user\Entity\Role; use Drupal\user\Entity\User; +use Drupal\user\UserInterface; /** * Tests the "Rendered item" processor. @@ -54,13 +60,31 @@ public function setUp($processor = NULL) { $this->installSchema('system', array('router')); \Drupal::service('router.builder')->rebuild(); - // Create a node type for testing. - $type = NodeType::create(array( - 'type' => 'page', - 'name' => 'page', - )); - $type->save(); - node_add_body_field($type); + // Creates node types for testing. + foreach (['article', 'page'] as $type_id) { + $type = NodeType::create(array( + 'type' => $type_id, + 'name' => $type_id, + )); + $type->save(); + node_add_body_field($type); + } + CommentType::create([ + 'id' => 'comment', + 'label' => 'comment', + 'target_entity_type_id' => 'node', + ])->save(); + FieldStorageConfig::create([ + 'field_name' => 'comment', + 'type' => 'comment', + 'entity_type' => 'node', + ])->save(); + FieldConfig::create([ + 'field_name' => 'comment', + 'entity_type' => 'node', + 'bundle' => 'page', + 'label' => 'Comments', + ])->save(); // Insert the anonymous user into the database. $anonymous_user = User::create(array( @@ -89,6 +113,12 @@ public function setUp($processor = NULL) { $node_data['body']['summary'] = 'summary for node 2'; $this->nodes[2] = Node::create($node_data); $this->nodes[2]->save(); + $node_data['type'] = 'article'; + $node_data['title'] = 'Title for node 3'; + $node_data['body']['value'] = 'value for node 3'; + $node_data['body']['summary'] = 'summary for node 3'; + $this->nodes[3] = Node::create($node_data); + $this->nodes[3]->save(); // Add a field based on the "rendered_item" property. $field_info = array( @@ -105,7 +135,7 @@ public function setUp($processor = NULL) { 'user' => 'compact', ), 'entity:comment' => array( - 'comment' => 'teaser', + 'comment' => 'full', ), ), ), @@ -113,9 +143,10 @@ public function setUp($processor = NULL) { $field = Utility::createField($this->index, 'rendered_item', $field_info); $this->index->addField($field); + $this->index->setDatasources($this->index->createPlugins('datasource')); + $this->index->save(); - $this->index->getDatasources(); // Enable the classy and stable themes as the tests rely on markup from // that. Set stable as the active theme, but make classy the default. The @@ -139,6 +170,29 @@ public function testAddFieldValues() { 'text' => 'node text' . $node->id(), ); } + $user = User::create([ + 'uid' => 2, + 'name' => 'User #2', + ]); + $items[] = [ + 'datasource' => 'entity:user', + 'item' => $user->getTypedData(), + 'item_id' => $user->id(), + ]; + $comment = Comment::create([ + 'entity_type' => 'node', + 'entity_id' => 1, + 'field_name' => 'comment', + 'cid' => 1, + 'comment_type' => 'comment', + 'subject' => 'Subject of comment 1', + ]); + $comment->save(); + $items[] = [ + 'datasource' => 'entity:comment', + 'item' => $comment->getTypedData(), + 'item_id' => $comment->id(), + ]; $items = $this->generateItems($items); // Add the processor's field values to the items. @@ -147,31 +201,96 @@ public function testAddFieldValues() { } foreach ($items as $key => $item) { - list(, $nid) = Utility::splitCombinedId($key); + list($datasource_id, $entity_id) = Utility::splitCombinedId($key); + $type = $this->index->getDatasource($datasource_id)->label(); + $field = $item->getField('rendered_item'); - $this->assertEquals('text', $field->getType(), 'Node item ' . $nid . ' rendered value is identified as text.'); + $this->assertEquals('text', $field->getType(), "$type item $entity_id rendered value is identified as text."); /** @var \Drupal\search_api\Plugin\search_api\data_type\value\TextValueInterface[] $values */ $values = $field->getValues(); // Test that the value is properly wrapped in a // \Drupal\search_api\Plugin\search_api\data_type\value\TextValueInterface // object, which contains a string (not, for example, some markup object). - $this->assertInstanceOf('Drupal\search_api\Plugin\search_api\data_type\value\TextValueInterface', $values[0], "Node item $nid rendered value is properly wrapped in a text value object."); - $this->assertInternalType('string', $values[0]->getText(), "Node item $nid rendered value is a string."); - $this->assertEquals(1, count($values), 'Node item ' . $nid . ' rendered value is a single value.'); - // These tests rely on the template not changing. However, if we'd only - // check whether the field values themselves are included, there could - // easier be false positives. For example, the title text was present even - // when the processor was broken, because the schema metadata was also - // adding it to the output. - $this->assertTrue(substr_count($values[0], 'view-mode-full') > 0, 'Node item ' . $nid . ' rendered in view-mode "full".'); - $this->assertTrue(substr_count($values[0], 'field--name-title') > 0, 'Node item ' . $nid . ' has a rendered title field.'); - $this->assertTrue(substr_count($values[0], '>' . $this->nodes[$nid]->label() . '<') > 0, 'Node item ' . $nid . ' has a rendered title inside HTML-Tags.'); - $this->assertTrue(substr_count($values[0], '>Member for<') > 0, 'Node item ' . $nid . ' has rendered member information HTML-Tags.'); - $this->assertTrue(substr_count($values[0], '>' . $this->nodes[$nid]->get('body')->getValue()[0]['value'] . '<') > 0, 'Node item ' . $nid . ' has rendered content inside HTML-Tags.'); + $this->assertInstanceOf('Drupal\search_api\Plugin\search_api\data_type\value\TextValueInterface', $values[0], "$type item $entity_id rendered value is properly wrapped in a text value object."); + $field_value = $values[0]->getText(); + $this->assertInternalType('string', $field_value, "$type item $entity_id rendered value is a string."); + $this->assertEquals(1, count($values), "$type item $entity_id rendered value is a single value."); + + switch ($datasource_id) { + case 'entity:node': + $this->checkRenderedNode($this->nodes[$entity_id], $field_value); + break; + + case 'entity:user': + $this->checkRenderedUser($user, $field_value); + break; + + case 'entity:comment': + $this->checkRenderedComment($comment, $field_value); + break; + + default: + $this->assertTrue(FALSE); + } + } } /** + * Verifies that a certain node has been rendered correctly. + * + * @param \Drupal\node\NodeInterface $node + * The node. + * @param string $field_value + * The rendered field value. + */ + protected function checkRenderedNode(NodeInterface $node, $field_value) { + // These tests rely on the template not changing. However, if we'd only + // check whether the field values themselves are included, there could + // easier be false positives. For example, the title text was present even + // when the processor was broken, because the schema metadata was also + // adding it to the output. + $nid = $node->id(); + $full_view = $node->bundle() === 'page'; + $view_mode = $full_view ? 'full' : 'teaser'; + $this->assertContains("view-mode-$view_mode", $field_value, 'Node item ' . $nid . " rendered in view-mode \"$view_mode\"."); + $this->assertContains('field--name-title', $field_value, 'Node item ' . $nid . ' has a rendered title field.'); + $this->assertContains('>' . $node->label() . '<', $field_value, 'Node item ' . $nid . ' has a rendered title inside HTML-Tags.'); + $this->assertContains('>Member for<', $field_value, 'Node item ' . $nid . ' has rendered member information HTML-Tags.'); + if ($full_view) { + $body_value = $node->get('body')->getValue()[0]['value'] . '<'; + } + else { + $body_value = $node->get('body')->getValue()[0]['summary'] . '<'; + } + $this->assertContains('>' . $body_value, $field_value, 'Node item ' . $nid . ' has rendered content inside HTML-Tags.'); + } + + /** + * Verifies that a certain user has been rendered correctly. + * + * @param \Drupal\user\UserInterface $user + * The user. + * @param string $field_value + * The rendered field value. + */ + protected function checkRenderedUser(UserInterface $user, $field_value) { + $this->assertContains('>Member for<', $field_value); + } + + /** + * Verifies that a certain comment has been rendered correctly. + * + * @param \Drupal\comment\CommentInterface $comment + * The comment. + * @param string $field_value + * The rendered field value. + */ + protected function checkRenderedComment(CommentInterface $comment, $field_value) { + $this->assertContains('>' . $comment->label() . '<', $field_value); + } + + /** * Tests that hiding a rendered item works. */ public function testHideRenderedItem() { @@ -236,6 +355,7 @@ public function testDependencyRemoval() { 'core.entity_view_mode.comment.full', 'core.entity_view_mode.node.full', 'core.entity_view_mode.node.teaser', + 'core.entity_view_mode.user.compact', ), ); $this->assertEquals($expected, $this->processor->calculateDependencies()); @@ -246,7 +366,7 @@ public function testDependencyRemoval() { 'page' => 'full', ), 'entity:user' => array( - 'user' => 'default', + 'user' => 'compact', ), 'entity:comment' => array( 'comment' => 'full', @@ -258,32 +378,4 @@ public function testDependencyRemoval() { $this->assertEquals($expected, $field_config['view_mode']); } - /** - * Tests that the processor reacts correctly when dependencies are removed. - */ - public function testOnDependencyRemoval() { - // Set the processor configuration to have the same view mode set for - // multiple bundles. - $config = $this->processor->getConfiguration(); - $config['view_mode']['entity:node']['page'] = 'teaser'; - $this->processor->setConfiguration($config); - - $view_mode_ids = array('node.teaser', 'user.compact'); - $dependencies = array(); - foreach ($view_mode_ids as $view_mode_id) { - $view_mode = EntityViewMode::load($view_mode_id); - $dependencies[$view_mode->getConfigDependencyKey()][$view_mode->getConfigDependencyName()] = $view_mode; - } - - $result = $this->processor->onDependencyRemoval($dependencies); - - $this->assertTrue($result, 'Dependency removal was successful.'); - $expected = array( - 'entity:comment' => array( - 'comment' => 'teaser', - ), - ); - $this->assertEquals($expected, $this->processor->getConfiguration()['view_mode'], 'Configuration was adapted correctly.'); - } - }