You are here

trait TestContentGenerationTrait in MixItUp Views 8.2

Contains helper functions for MixItUp Views Kernel tests.

Hierarchy

File

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

Namespace

Drupal\Tests\mixitup_views\Kernel
View source
trait TestContentGenerationTrait {

  /**
   * Generates test content for Kernel tests.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TestContentGenerationTrait::generateTestContent public function Generates test content for Kernel tests.