You are here

public function PremiumContentTest::setUp in Node Option Premium 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/Plugin/search_api/processor/PremiumContentTest.php, line 51

Class

PremiumContentTest
Tests the "Premium content" processor.

Namespace

Drupal\Tests\nopremium\Kernel\Plugin\search_api\processor

Code

public function setUp($processor = NULL) {
  parent::setUp('nopremium');
  $this
    ->installConfig([
    'nopremium',
  ]);

  // Create a node type for testing.
  $type = NodeType::create([
    'type' => 'article',
    'name' => 'Article',
  ]);
  $type
    ->save();

  // Create anonymous user role.
  $role = Role::create([
    'id' => 'anonymous',
    'label' => 'anonymous',
  ]);
  $role
    ->save();

  // Create a premium node.
  $values = [
    'status' => NodeInterface::PUBLISHED,
    'type' => 'article',
    'title' => 'Premium item',
    'premium' => TRUE,
  ];
  $this->nodes[0] = Node::create($values);
  $this->nodes[0]
    ->save();

  // Create a non-premium node.
  $values = [
    'status' => NodeInterface::PUBLISHED,
    'type' => 'article',
    'title' => 'Free item',
    'premium' => FALSE,
  ];
  $this->nodes[1] = Node::create($values);
  $this->nodes[1]
    ->save();

  // Create a node index.
  $datasources = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createDatasourcePlugins($this->index, [
    'entity:node',
  ]);
  $this->index
    ->setDatasources($datasources);
  $this->index
    ->save();

  // And index items.
  \Drupal::getContainer()
    ->get('search_api.index_task_manager')
    ->addItemsAll($this->index);
  $index_storage = \Drupal::entityTypeManager()
    ->getStorage('search_api_index');
  $index_storage
    ->resetCache([
    $this->index
      ->id(),
  ]);
  $this->index = $index_storage
    ->load($this->index
    ->id());
}