You are here

public function MixItUpFuncTest::setUp in MixItUp Views 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/MixItUpFuncTest.php \Drupal\Tests\mixitup_views\Kernel\MixItUpFuncTest::setUp()

Before a test method is run, setUp() is invoked.

Create new unit object.

Throws

\Drupal\Core\Entity\EntityStorageException

Overrides KernelTestBase::setUp

File

tests/src/Kernel/MixItUpFuncTest.php, line 64

Class

MixItUpFuncTest
Class MixItUpFuncTest.

Namespace

Drupal\Tests\mixitup_views\Kernel

Code

public function setUp() {
  parent::setUp();

  // Installing entities schema.
  $this
    ->installSchema('system', [
    'sequences',
    'key_value_expire',
  ]);
  $this
    ->installEntitySchema('user');
  $this
    ->installEntitySchema('taxonomy_term');
  $this
    ->installEntitySchema('node');

  // Creating instance for class which will be tested.
  $this->entityTypeManager = \Drupal::entityTypeManager();
  $this->defaultOptions = new MixitupViewsDefaultOptionsService();
  $this->unit = new MixitupFunc($this->defaultOptions, $this->entityTypeManager);
  for ($i = 0; $i < 5; $i++) {

    // Creating the user.
    $user = 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 = Term::create([
      'vid' => 'tags',
    ]);
    $taxonomy
      ->setName('test name' . strval($i));
    $taxonomy
      ->save();

    // Creating node.
    $node = 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 = \Drupal::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();
}