You are here

public function RenderedItemTest::setUp in Search API 8

Performs setup tasks before each individual test method is run.

Installs commonly used schemas and sets up a search server and an index, with the specified processor enabled.

Parameters

string|null $processor: (optional) The plugin ID of the processor that should be set up for testing.

Overrides ProcessorTestBase::setUp

File

tests/src/Kernel/Processor/RenderedItemTest.php, line 58

Class

RenderedItemTest
Tests the "Rendered item" processor.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

public function setUp($processor = NULL) {
  parent::setUp('rendered_item');

  // Enable the optional "path_alias" entity type as well to make sure the
  // processor doesn't break for any of the default types.
  $this
    ->installEntitySchema('path_alias');

  // Load additional configuration and needed schemas. (The necessary schemas
  // for using nodes are already installed by the parent method.)
  $this
    ->installConfig([
    'system',
    'filter',
    'node',
    'comment',
    'user',
  ]);
  \Drupal::service('router.builder')
    ->rebuild();

  // Create the default languages and a new one for translations.
  $this
    ->installConfig([
    'language',
  ]);

  /** @var \Drupal\language\Entity\ConfigurableLanguage $language */
  $language = ConfigurableLanguage::create([
    'id' => 'de',
    'label' => 'German',
    'weight' => 0,
  ]);
  $language
    ->save();

  // Creates node types for testing.
  foreach ([
    'article',
    'page',
  ] as $type_id) {
    $type = NodeType::create([
      '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([
    'uid' => 0,
    'name' => '',
  ]);
  $anonymous_user
    ->save();

  // Default node values for all nodes we create below.
  $node_data = [
    'status' => NodeInterface::PUBLISHED,
    'type' => 'page',
    'title' => '',
    'body' => [
      'value' => '',
      'summary' => '',
      'format' => 'plain_text',
    ],
    'uid' => $anonymous_user
      ->id(),
  ];

  // Create some test nodes with valid user on it for rendering a picture.
  $node_data['title'] = 'Title for node 1';
  $node_data['body']['value'] = 'value for node 1';
  $node_data['body']['summary'] = 'summary for node 1';
  $this->nodes[1] = Node::create($node_data);
  $this->nodes[1]
    ->save();
  $node_data['title'] = 'Title for node 2';
  $node_data['body']['value'] = 'value for node 2';
  $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 = [
    'type' => 'text',
    'property_path' => 'rendered_item',
    'configuration' => [
      'roles' => [
        'anonymous',
      ],
      'view_mode' => [
        'entity:node' => [
          'page' => 'full',
          'article' => 'teaser',
        ],
        'entity:user' => [
          'user' => 'compact',
        ],
        'entity:comment' => [
          'comment' => 'full',
        ],
      ],
    ],
  ];
  $field = \Drupal::getContainer()
    ->get('search_api.fields_helper')
    ->createField($this->index, 'rendered_item', $field_info);
  $this->index
    ->addField($field);
  $datasources = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createDatasourcePlugins($this->index);
  $this->index
    ->setDatasources($datasources);
  $this->index
    ->save();

  // 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
  // processor should switch to classy to perform the rendering.
  \Drupal::service('theme_installer')
    ->install([
    'classy',
  ]);
  \Drupal::service('theme_installer')
    ->install([
    'stable',
  ]);
  \Drupal::configFactory()
    ->getEditable('system.theme')
    ->set('default', 'classy')
    ->save();
  \Drupal::theme()
    ->setActiveTheme(\Drupal::service('theme.initialization')
    ->initTheme('stable'));
}