You are here

public function TestContentGenerationTrait::generateTestContent in MixItUp Views 8.2

Generates test content for Kernel tests.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

2 calls to TestContentGenerationTrait::generateTestContent()
MixitUpFiltersFormTest::setUp in tests/src/Kernel/MixitUpFiltersFormTest.php
Before a test method is run, setUp() is invoked.
MixItUpFuncTest::setUp in tests/src/Kernel/MixItUpFuncTest.php
Before a test method is run, setUp() is invoked.

File

tests/src/Kernel/TestContentGenerationTrait.php, line 17

Class

TestContentGenerationTrait
Contains helper functions for MixItUp Views Kernel tests.

Namespace

Drupal\Tests\mixitup_views\Kernel

Code

public function generateTestContent() {

  // Creating vocabulary.
  $this->entityTypeManager
    ->getStorage('taxonomy_vocabulary')
    ->create([
    'vid' => 'tags',
    'name' => 'Tags',
    'description' => 'Tags',
  ])
    ->save();
  for ($i = 0; $i < 5; $i++) {

    // Creating the user.
    $user = $this->entityTypeManager
      ->getStorage('user')
      ->create([
      'uid' => $i,
      'name' => 'user_name' . strval($i),
      'mail' => 'email' . strval($i) . '@email.com',
      'password' => 'password',
      'status' => '1',
      'role' => '1',
    ]);
    $user
      ->save();

    // Creating taxonomy term.
    $taxonomy = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->create([
      'vid' => 'tags',
    ]);
    $taxonomy
      ->setName('test name' . strval($i));
    $taxonomy
      ->save();

    // Creating node.
    $node = $this->entityTypeManager
      ->getStorage('node')
      ->create([
      'type' => 'article',
      'title' => 'Test node #' . strval($i),
      'uid' => $user
        ->id(),
      'tags' => [
        0 => [
          'target_id' => $taxonomy
            ->id(),
        ],
      ],
    ]);
    $node
      ->save();
  }

  // Adding test data to taxonomy_index table.
  $taxonomy_index_query = $this->serviceContainer
    ->get('database')
    ->insert('taxonomy_index')
    ->fields([
    'nid',
    'tid',
    'status',
    'sticky',
    'created',
  ]);
  $taxonomy_index_query
    ->values([
    '5',
    '2',
    '1',
    '0',
    time(),
  ])
    ->execute();
  $taxonomy_index_query
    ->values([
    '5',
    '3',
    '1',
    '0',
    time(),
  ])
    ->execute();
  $taxonomy_index_query
    ->values([
    '4',
    '1',
    '1',
    '0',
    time(),
  ])
    ->execute();
  $taxonomy_index_query
    ->values([
    '4',
    '4',
    '1',
    '0',
    time(),
  ])
    ->execute();
  $taxonomy_index_query
    ->values([
    '3',
    '1',
    '1',
    '0',
    time(),
  ])
    ->execute();
  $taxonomy_index_query
    ->values([
    '3',
    '5',
    '1',
    '0',
    time(),
  ])
    ->execute();
  $taxonomy_index_query
    ->values([
    '2',
    '3',
    '1',
    '0',
    time(),
  ])
    ->execute();
  $taxonomy_index_query
    ->values([
    '2',
    '4',
    '1',
    '0',
    time(),
  ])
    ->execute();
  $taxonomy_index_query
    ->values([
    '1',
    '2',
    '1',
    '0',
    time(),
  ])
    ->execute();
  $taxonomy_index_query
    ->values([
    '1',
    '4',
    '1',
    '0',
    time(),
  ])
    ->execute();
}