TaxonomyTermIndentationTest.php in Drupal 9
File
core/modules/taxonomy/tests/src/Functional/TaxonomyTermIndentationTest.php
View source
<?php
namespace Drupal\Tests\taxonomy\Functional;
class TaxonomyTermIndentationTest extends TaxonomyTestBase {
protected static $modules = [
'taxonomy',
];
protected $defaultTheme = 'stark';
protected $vocabulary;
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'administer taxonomy',
'bypass node access',
]));
$this->vocabulary = $this
->createVocabulary();
}
public function testTermIndentation() {
$assert = $this
->assertSession();
$term1 = $this
->createTerm($this->vocabulary);
$term2 = $this
->createTerm($this->vocabulary);
$term3 = $this
->createTerm($this->vocabulary);
$taxonomy_storage = $this->container
->get('entity_type.manager')
->getStorage('taxonomy_term');
$this
->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
->get('vid') . '/overview');
$hidden_edit = [
'terms[tid:' . $term2
->id() . ':0][term][tid]' => 2,
'terms[tid:' . $term2
->id() . ':0][term][parent]' => 1,
'terms[tid:' . $term2
->id() . ':0][term][depth]' => 1,
];
foreach ($hidden_edit as $field => $value) {
$node = $assert
->hiddenFieldExists($field);
$node
->setValue($value);
}
$edit = [
'terms[tid:' . $term2
->id() . ':0][weight]' => 1,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->responseMatches('|<div class="js-indentation indentation"> </div>|');
$parents = $taxonomy_storage
->loadParents($term2
->id());
$this
->assertEquals(1, key($parents), 'Term 1 is the term 2\'s parent');
$this
->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
->get('vid') . '/overview');
$hidden_edit = [
'terms[tid:' . $term2
->id() . ':0][term][tid]' => 2,
'terms[tid:' . $term2
->id() . ':0][term][parent]' => 0,
'terms[tid:' . $term2
->id() . ':0][term][depth]' => 0,
];
foreach ($hidden_edit as $field => $value) {
$node = $assert
->hiddenFieldExists($field);
$node
->setValue($value);
}
$edit = [
'terms[tid:' . $term2
->id() . ':0][weight]' => 1,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->responseNotMatches('|<div class="js-indentation indentation"> </div>|');
\Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->resetCache();
$parents = $taxonomy_storage
->loadParents($term2
->id());
$this
->assertTrue(empty($parents), 'Term 2 has no parents now');
}
}