View source
<?php
namespace Drupal\Tests\taxonomy_term_depth\Functional;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\BrowserTestBase;
class DynamicDepthCalculationTest extends BrowserTestBase {
public static $modules = [
'taxonomy',
'taxonomy_term_depth',
];
protected $defaultTheme = 'stable';
protected function setUp() {
parent::setUp();
}
public function testCalculateDepth() {
$voc = Vocabulary::create([
'vid' => 'testvoc',
'name' => 'testvoc',
'description' => 'testvoc',
]);
$voc
->save();
$term1 = Term::create([
'vid' => $voc
->id(),
'name' => 'Depth 1 term',
]);
$term1
->save();
$term2 = Term::create([
'vid' => $voc
->id(),
'name' => 'Depth 2 term',
]);
$term2->parent
->set(0, $term1
->id());
$term2
->save();
$term3 = Term::create([
'vid' => $voc
->id(),
'name' => 'Depth 2 term',
]);
$term3->parent
->set(0, $term3
->id());
$term3->parent
->set(1, $term2
->id());
$term3
->save();
$this
->assertEqual(taxonomy_term_depth_get_by_tid($term1
->id()), 1, 'Depth of first term');
$this
->assertEqual(taxonomy_term_depth_get_by_tid($term2
->id()), 2, 'Depth of second term');
$this
->assertEqual(taxonomy_term_depth_get_by_tid($term3
->id()), 3, 'Depth of third term');
$this
->assertEqual($term1->depth_level
->first() ? $term1->depth_level
->first()->value : NULL, 1, 'Saved depth of first term');
$this
->assertEqual($term2->depth_level
->first() ? $term2->depth_level
->first()->value : NULL, 2, 'Saved depth of second term');
$this
->assertEqual($term3->depth_level
->first() ? $term3->depth_level
->first()->value : NULL, 3, 'Saved depth of third term');
$chain = taxonomy_term_depth_get_full_chain($term2
->id());
$compare = [
$term1
->id(),
$term2
->id(),
$term3
->id(),
];
$this
->assertTrue($chain === $compare, 'Testing fullchain for term2');
$chain = taxonomy_term_depth_get_full_chain($term2
->id(), TRUE);
$this
->assertTrue($chain === array_reverse($compare), 'Testing reversed fullchain for term2');
$this
->assertEqual(\Drupal::database()
->query('SELECT depth_level FROM {taxonomy_term_field_data} WHERE tid=:tid', [
':tid' => $term1
->id(),
])
->fetchField(), 1, 'DB depth_level field of first term');
$this
->assertEqual(\Drupal::database()
->query('SELECT depth_level FROM {taxonomy_term_field_data} WHERE tid=:tid', [
':tid' => $term2
->id(),
])
->fetchField(), 2, 'DB depth_level field of second term');
$this
->assertEqual(\Drupal::database()
->query('SELECT depth_level FROM {taxonomy_term_field_data} WHERE tid=:tid', [
':tid' => $term3
->id(),
])
->fetchField(), 3, 'DB depth_level field of third term');
}
public function testCronQueue() {
$this
->assertTrue(TRUE, 'Clearing all depths and running cron to update, then checking again');
taxonomy_term_depth_queue_manager()
->clearDepths();
$this
->_cronRun();
$this
->testCalculateDepth();
}
protected function _cronRun() {
$this
->drupalGet('cron/' . \Drupal::state()
->get('system.cron_key'));
}
}