View source
<?php
namespace Drupal\Tests\hierarchical_taxonomy_menu\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class HierarchicalTaxonomyMenuBasicTest extends WebDriverTestBase {
use BlockCreationTrait;
use TaxonomyTestTrait;
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->block = $this
->drupalPlaceBlock('hierarchical_taxonomy_menu', [
'region' => 'content',
'label' => 'Hierarchical Taxonomy Menu',
'id' => 'hierarchicaltaxonomymenu',
]);
}
public function testZeroDepth() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'max_depth' => 0,
]);
$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()
->pageTextNotContains('Child term ' . $i . '-' . $n);
}
}
}
public function testUnlimitedDepth() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'max_depth' => 100,
]);
$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);
}
}
}
public function testDynamicBlockTitleEnabled() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'dynamic_block_title' => TRUE,
]);
$this->block
->save();
$this
->drupalGet('taxonomy/term/1');
$block_title_element = $this
->getSession()
->getPage()
->find('css', '#block-hierarchicaltaxonomymenu h2');
$this
->assertEqual($block_title_element
->getText(), 'Parent term 1');
}
public function testDynamicBlockTitleDisabled() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'dynamic_block_title' => FALSE,
]);
$this->block
->save();
$this
->drupalGet('taxonomy/term/1');
$block_title_element = $this
->getSession()
->getPage()
->find('css', '#block-hierarchicaltaxonomymenu h2');
$this
->assertEqual($block_title_element
->getText(), 'Hierarchical Taxonomy Menu');
}
public function testCollapsed() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'collapsible' => 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()
->pageTextNotContains('Child term ' . $i . '-' . $n);
}
}
}
public function testNotCollapsed() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'collapsible' => FALSE,
]);
$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);
}
}
}
public function testCollapsedAndStayOpen() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'collapsible' => TRUE,
'stay_open' => TRUE,
]);
$this->block
->save();
$this
->drupalGet('taxonomy/term/12');
for ($i = 1; $i < 4; $i++) {
$this
->assertSession()
->pageTextContains('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);
}
}
}
}
public function testCollapsedAndStayClosed() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'collapsible' => TRUE,
'stay_open' => FALSE,
]);
$this->block
->save();
$this
->drupalGet('taxonomy/term/6');
for ($i = 1; $i < 4; $i++) {
$this
->assertSession()
->pageTextContains('Parent term ' . $i);
for ($n = 1; $n < 12; $n++) {
$this
->assertSession()
->pageTextNotContains('Child term ' . $i . '-' . $n);
}
}
}
public function testCollapsedAndSelectable() {
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $this->vocabulary
->id() . '|',
'collapsible' => TRUE,
'interactive_parent' => TRUE,
]);
$this->block
->save();
$this
->drupalGet('<front>');
$toggle_elements = $this
->getSession()
->getPage()
->findAll('css', '#block-hierarchicaltaxonomymenu i.arrow-right.parent-toggle');
$this
->assertCount(33, $toggle_elements);
}
public function testEmptyBlock() {
$vocabulary = $this
->createVocabulary();
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $vocabulary
->id() . '|',
'hide_block' => FALSE,
]);
$this->block
->save();
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextContains('Hierarchical Taxonomy Menu');
$this->block
->set('settings', [
'label' => 'Hierarchical Taxonomy Menu',
'label_display' => 'visible',
'vocabulary' => $vocabulary
->id() . '|',
'hide_block' => TRUE,
]);
$this->block
->save();
$this
->drupalGet('<front>');
$this
->assertSession()
->pageTextNotContains('Hierarchical Taxonomy Menu');
}
}