View source
<?php
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\TermInterface;
use Drupal\Tests\BrowserTestBase;
class TermParentsTest extends BrowserTestBase {
protected static $modules = [
'taxonomy_test',
];
protected $defaultTheme = 'stark';
protected $termStorage;
protected $state;
protected $vocabularyId = 'test_vocabulary';
protected function setUp() : void {
parent::setUp();
$entity_type_manager = $this->container
->get('entity_type.manager');
$this->termStorage = $entity_type_manager
->getStorage('taxonomy_term');
$this->state = $this->container
->get('state');
Vocabulary::create([
'vid' => $this->vocabularyId,
])
->save();
$this
->drupalLogin($this
->drupalCreateUser([
'administer taxonomy',
]));
}
public function testAddWithParents() {
$this
->drupalGet("/admin/structure/taxonomy/manage/{$this->vocabularyId}/add");
$page = $this
->getSession()
->getPage();
$term_1 = $this
->submitAddTermForm('Test term 1');
$expected = [
[
'target_id' => 0,
],
];
$this
->assertEquals($expected, $term_1
->get('parent')
->getValue());
$page
->selectFieldOption('Parent terms', '<root>');
$term_2 = $this
->submitAddTermForm('Test term 2');
$this
->assertEquals($expected, $term_2
->get('parent')
->getValue());
$page
->selectFieldOption('Parent terms', 'Test term 1');
$term_3 = $this
->submitAddTermForm('Test term 3');
$expected = [
[
'target_id' => $term_1
->id(),
],
];
$this
->assertEquals($expected, $term_3
->get('parent')
->getValue());
$page
->selectFieldOption('Parent terms', 'Test term 2');
$term_4 = $this
->submitAddTermForm('Test term 4');
$expected = [
[
'target_id' => $term_2
->id(),
],
];
$this
->assertEquals($expected, $term_4
->get('parent')
->getValue());
$page
->selectFieldOption('Parent terms', '-Test term 3');
$term_5 = $this
->submitAddTermForm('Test term 5');
$expected = [
[
'target_id' => $term_3
->id(),
],
];
$this
->assertEquals($expected, $term_5
->get('parent')
->getValue());
$page
->selectFieldOption('Parent terms', '--Test term 5');
$page
->selectFieldOption('Parent terms', '-Test term 4', TRUE);
$term_6 = $this
->submitAddTermForm('Test term 6');
$expected = [
[
'target_id' => $term_5
->id(),
],
[
'target_id' => $term_4
->id(),
],
];
$this
->assertEquals($expected, $term_6
->get('parent')
->getValue());
}
protected function submitAddTermForm($name) {
$this
->getSession()
->getPage()
->fillField('Name', $name);
$this
->submitForm([], 'Save');
$result = $this->termStorage
->getQuery()
->accessCheck(FALSE)
->condition('name', $name)
->execute();
$term_1 = $this->termStorage
->load(reset($result));
$this
->assertInstanceOf(TermInterface::class, $term_1);
return $term_1;
}
public function testEditingParents() {
$terms = $this
->doTestEditingSingleParent();
$term_5 = array_pop($terms);
$term_4 = array_pop($terms);
$term_6 = $this
->createTerm('Test term 6', [
$term_5
->id(),
$term_4
->id(),
]);
$this
->drupalGet($term_6
->toUrl('edit-form'));
$this
->assertParentOption('<root>');
$this
->assertParentOption('Test term 1');
$this
->assertParentOption('-Test term 3');
$this
->assertParentOption('--Test term 5', TRUE);
$this
->assertParentOption('Test term 2');
$this
->assertParentOption('-Test term 4', TRUE);
$this
->submitForm([], 'Save');
$this
->assertParentsUnchanged($term_6);
}
public function testEditingParentsWithDisabledFormElement() {
$this->state
->set('taxonomy_test.disable_parent_form_element', TRUE);
$this
->drupalGet("/admin/structure/taxonomy/manage/{$this->vocabularyId}/add");
$this
->assertSession()
->fieldDisabled('Parent terms');
$terms = $this
->doTestEditingSingleParent();
$term_5 = array_pop($terms);
$term_4 = array_pop($terms);
$term_6 = $this
->createTerm('Test term 6', [
$term_4
->id(),
$term_5
->id(),
]);
$this
->drupalGet($term_6
->toUrl('edit-form'));
$this
->assertParentOption('<root>');
$this
->assertParentOption('Test term 1');
$this
->assertParentOption('-Test term 3');
$this
->assertParentOption('--Test term 5', TRUE);
$this
->assertParentOption('Test term 2');
$this
->assertParentOption('-Test term 4', TRUE);
$this
->submitForm([], 'Save');
$this
->assertParentsUnchanged($term_6);
}
protected function doTestEditingSingleParent() {
$terms = [];
$term_1 = $this
->createTerm('Test term 1');
$this
->drupalGet($term_1
->toUrl('edit-form'));
$this
->assertParentOption('<root>', TRUE);
$this
->submitForm([], 'Save');
$this
->assertParentsUnchanged($term_1);
$terms[] = $term_1;
$term_2 = $this
->createTerm('Test term 2');
$this
->drupalGet($term_2
->toUrl('edit-form'));
$this
->assertParentOption('<root>', TRUE);
$this
->assertParentOption('Test term 1');
$this
->submitForm([], 'Save');
$this
->assertParentsUnchanged($term_2);
$terms[] = $term_2;
$term_3 = $this
->createTerm('Test term 3', [
$term_1
->id(),
]);
$this
->drupalGet($term_3
->toUrl('edit-form'));
$this
->assertParentOption('<root>');
$this
->assertParentOption('Test term 1', TRUE);
$this
->assertParentOption('Test term 2');
$this
->submitForm([], 'Save');
$this
->assertParentsUnchanged($term_3);
$terms[] = $term_3;
$term_4 = $this
->createTerm('Test term 4', [
$term_2
->id(),
]);
$this
->drupalGet($term_4
->toUrl('edit-form'));
$this
->assertParentOption('<root>');
$this
->assertParentOption('Test term 1');
$this
->assertParentOption('-Test term 3');
$this
->assertParentOption('Test term 2', TRUE);
$this
->submitForm([], 'Save');
$this
->assertParentsUnchanged($term_4);
$terms[] = $term_4;
$term_5 = $this
->createTerm('Test term 5', [
$term_3
->id(),
]);
$this
->drupalGet($term_5
->toUrl('edit-form'));
$this
->assertParentOption('<root>');
$this
->assertParentOption('Test term 1');
$this
->assertParentOption('-Test term 3', TRUE);
$this
->assertParentOption('Test term 2');
$this
->assertParentOption('-Test term 4');
$this
->submitForm([], 'Save');
$this
->assertParentsUnchanged($term_5);
$terms[] = $term_5;
return $terms;
}
protected function createTerm($name, array $parent_ids = []) {
$term = $this->termStorage
->create([
'name' => $name,
'vid' => $this->vocabularyId,
]);
foreach ($parent_ids as $delta => $parent_id) {
$term
->get('parent')
->set($delta, [
'target_id' => $parent_id,
]);
}
$term
->save();
return $term;
}
protected function assertParentOption($option, $selected = FALSE) {
$option = $this
->assertSession()
->optionExists('Parent terms', $option);
if ($selected) {
$this
->assertTrue($option
->hasAttribute('selected'));
}
else {
$this
->assertFalse($option
->hasAttribute('selected'));
}
}
protected function assertParentsUnchanged(TermInterface $term) {
$saved_term = $this->termStorage
->load($term
->id());
$expected = $term
->get('parent')
->getValue();
$this
->assertEquals($expected, $saved_term
->get('parent')
->getValue());
}
}