View source
<?php
namespace Drupal\Tests\mixitup_views\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\mixitup_views\MixitupFunc;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\mixitup_views\MixitupViewsDefaultOptionsService;
use Drupal\user\Entity\User;
class MixItUpFuncTest extends KernelTestBase {
protected $unit;
protected $entityTypeManager;
protected $defaultOptions;
public static $modules = [
'system',
'field',
'text',
'entity_reference',
'user',
'node',
'taxonomy',
'mixitup_views',
];
public function setUp() {
parent::setUp();
$this
->installSchema('system', [
'sequences',
'key_value_expire',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('taxonomy_term');
$this
->installEntitySchema('node');
$this->entityTypeManager = \Drupal::entityTypeManager();
$this->defaultOptions = new MixitupViewsDefaultOptionsService();
$this->unit = new MixitupFunc($this->defaultOptions, $this->entityTypeManager);
for ($i = 0; $i < 5; $i++) {
$user = User::create([
'uid' => $i,
'name' => 'user_name' . strval($i),
'mail' => 'email' . strval($i) . '@email.com',
'password' => 'password',
'status' => '1',
'role' => '1',
]);
$user
->save();
$taxonomy = Term::create([
'vid' => 'tags',
]);
$taxonomy
->setName('test name' . strval($i));
$taxonomy
->save();
$node = Node::create([
'type' => 'article',
'title' => 'Test node #' . strval($i),
'uid' => $user
->id(),
'tags' => [
0 => [
'target_id' => $taxonomy
->id(),
],
],
]);
$node
->save();
}
$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();
}
public function testGetRowClasses() {
self::assertEquals('tid_2 tid_3', $this->unit
->getRowClasses(5));
self::assertEquals('tid_1 tid_4', $this->unit
->getRowClasses(4));
self::assertEquals('tid_1 tid_5', $this->unit
->getRowClasses(3));
self::assertEquals('tid_3 tid_4', $this->unit
->getRowClasses(2));
self::assertEquals('tid_2 tid_4', $this->unit
->getRowClasses(1));
}
public function testGetNodeTids() {
self::assertEquals([
0 => 2,
1 => 3,
], $this->unit
->getNodeTids(5));
self::assertEquals([
0 => 1,
1 => 4,
], $this->unit
->getNodeTids(4));
self::assertEquals([
0 => 1,
1 => 5,
], $this->unit
->getNodeTids(3));
self::assertEquals([
0 => 3,
1 => 4,
], $this->unit
->getNodeTids(2));
self::assertEquals([
0 => 2,
1 => 4,
], $this->unit
->getNodeTids(1));
}
public function tearDown() {
unset($this->unit);
}
}