View source
<?php
namespace Drupal\Tests\hierarchical_taxonomy_menu\FunctionalJavascript;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class HierarchicalTaxonomyMenuAdvancedTest extends WebDriverTestBase {
use BlockCreationTrait;
use TaxonomyTestTrait;
use NodeCreationTrait;
use EntityReferenceTestTrait;
public static $modules = [
'block',
'image',
'hierarchical_taxonomy_menu',
'taxonomy',
];
protected $defaultTheme = 'stark';
protected $vocabulary;
protected $childParent;
protected $block;
protected function setUp() {
parent::setUp();
$admin_user = $this
->drupalCreateUser([
'administer blocks',
'administer site configuration',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$this->vocabulary = $this
->createVocabulary();
for ($i = 1; $i < 4; $i++) {
$parent = Term::create([
'vid' => $this->vocabulary
->id(),
'name' => 'Parent term ' . $i,
]);
$parent
->save();
for ($n = 1; $n < 12; $n++) {
$child = Term::create([
'vid' => $this->vocabulary
->id(),
'name' => 'Child term ' . $i . '-' . $n,
]);
if ($n == 1) {
$child->parent = $parent
->id();
}
else {
$child->parent = $this->childParent;
}
$child
->save();
$this->childParent = $child
->id();
}
}
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->createEntityReferenceField('node', 'article', 'field_tags', 'Term reference', 'taxonomy_term', 'default', [
'target_bundles' => [
$this->vocabulary
->id(),
],
], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$this->block = $this
->drupalPlaceBlock('hierarchical_taxonomy_menu', [
'region' => 'content',
'label' => 'Hierarchical Taxonomy Menu',
'id' => 'hierarchicaltaxonomymenu',
]);
}
public function testBaseTerm() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'base_term' => 3,
]);
$this->block
->save();
$this
->drupalGet('<front>');
for ($i = 1; $i < 4; $i++) {
$this
->assertSession()
->pageTextNotContains('Parent term ' . $i);
for ($n = 1; $n < 12; $n++) {
if ($i == 1 && $n > 2) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n);
}
else {
$this
->assertSession()
->pageTextNotContains('Child term ' . $i . '-' . $n . ' ');
}
}
}
}
public function testDynamicBaseTerm() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'dynamic_base_term' => TRUE,
]);
$this->block
->save();
$this
->drupalGet('<front>');
for ($i = 1; $i < 4; $i++) {
$this
->assertSession()
->pageTextContains('Parent term ' . $i);
for ($n = 1; $n < 12; $n++) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n);
}
}
$this
->drupalGet('taxonomy/term/1');
for ($i = 1; $i < 4; $i++) {
$this
->assertSession()
->pageTextNotContains('Parent term ' . $i);
for ($n = 1; $n < 12; $n++) {
if ($i == 1) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n);
}
else {
$this
->assertSession()
->pageTextNotContains('Child term ' . $i . '-' . $n);
}
}
}
$this
->drupalGet('taxonomy/term/13');
for ($i = 1; $i < 4; $i++) {
$this
->assertSession()
->pageTextNotContains('Parent term ' . $i);
for ($n = 1; $n < 12; $n++) {
if ($i == 2) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n);
}
else {
$this
->assertSession()
->pageTextNotContains('Child term ' . $i . '-' . $n);
}
}
}
$this
->drupalGet('taxonomy/term/25');
for ($i = 1; $i < 4; $i++) {
$this
->assertSession()
->pageTextNotContains('Parent term ' . $i);
for ($n = 1; $n < 12; $n++) {
if ($i == 3) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n);
}
else {
$this
->assertSession()
->pageTextNotContains('Child term ' . $i . '-' . $n);
}
}
}
$this
->drupalGet('taxonomy/term/33');
for ($i = 1; $i < 4; $i++) {
$this
->assertSession()
->pageTextNotContains('Parent term ' . $i);
for ($n = 1; $n < 12; $n++) {
if ($i == 3 && $n > 8) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n);
}
else {
$this
->assertSession()
->pageTextNotContains('Child term ' . $i . '-' . $n . ' ');
}
}
}
}
public function testCountOfReferencingNodes() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'show_count' => TRUE,
]);
$this->block
->save();
$this
->drupalGet('<front>');
for ($i = 1; $i < 4; $i++) {
$this
->assertSession()
->pageTextContains('Parent term ' . $i . ' [0]');
for ($n = 1; $n < 12; $n++) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n . ' [0]');
}
}
$term = Term::load(1);
$this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article 1',
'field_tags' => $term,
]);
$term = Term::load(2);
$this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article 2',
'field_tags' => $term,
]);
$term = Term::load(34);
$this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article 3',
'field_tags' => $term,
]);
$this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article 4',
'field_tags' => $term,
]);
$this
->drupalGet('<front>');
for ($i = 1; $i < 4; $i++) {
if ($i == 1) {
$this
->assertSession()
->pageTextContains('Parent term ' . $i . ' [1]');
}
else {
$this
->assertSession()
->pageTextContains('Parent term ' . $i . ' [0]');
}
for ($n = 1; $n < 12; $n++) {
if ($i == 1 && $n == 1) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n . ' [1]');
}
elseif ($i == 3 && $n == 9) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n . ' [2]');
}
else {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n . ' [0]');
}
}
}
}
public function testRecursivelyCountOfReferencingNodes() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'show_count' => TRUE,
'calculate_count_recursively' => TRUE,
]);
$this->block
->save();
$this
->drupalGet('<front>');
for ($i = 1; $i < 4; $i++) {
$this
->assertSession()
->pageTextContains('Parent term ' . $i . ' [0]');
for ($n = 1; $n < 12; $n++) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n . ' [0]');
}
}
$term = Term::load(1);
$this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article 1',
'field_tags' => $term,
]);
$term = Term::load(2);
$this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article 2',
'field_tags' => $term,
]);
$term = Term::load(3);
$this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article 3',
'field_tags' => $term,
]);
$this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article 4',
'field_tags' => $term,
]);
$term = Term::load(4);
$this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article 5',
'field_tags' => $term,
]);
$this
->drupalCreateNode([
'type' => 'article',
'title' => 'Article 6',
'field_tags' => $term,
]);
$this
->drupalGet('<front>');
for ($i = 1; $i < 4; $i++) {
if ($i == 1) {
$this
->assertSession()
->pageTextContains('Parent term ' . $i . ' [6]');
}
else {
$this
->assertSession()
->pageTextContains('Parent term ' . $i . ' [0]');
}
for ($n = 1; $n < 12; $n++) {
if ($i == 1 && $n == 1) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n . ' [5]');
}
elseif ($i == 1 && $n == 2) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n . ' [4]');
}
elseif ($i == 1 && $n == 3) {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n . ' [2]');
}
else {
$this
->assertSession()
->pageTextContains('Child term ' . $i . '-' . $n . ' [0]');
}
}
}
}
}