ShsTermTest.php in Simple hierarchical select 2.0.x
File
tests/src/Functional/ShsTermTest.php
View source
<?php
namespace Drupal\Tests\shs\Functional;
use Drupal\Tests\shs\Functional\ShsTestTrait;
class ShsTermTest extends ShsTestBase {
use ShsTestTrait;
public function testFirstLevel() : void {
$expected = array_filter($this
->getTermStructure(), function ($parent) {
return empty($parent);
});
$field_name = 'shs-' . strtr($this->fieldName, [
'_' => '-',
]);
$request_url = "shs-term-data/{$field_name}/{$this->vocabulary->id()}/0";
$data = $this
->drupalGetJson($request_url);
$expected_count = count($expected);
$this
->assertCount($expected_count, $data, "JSON callback returned {$expected_count} result");
$terms_with_children_expected = [
'aaa 1',
'aaa 3',
];
$keys = array_flip(array_keys(array_column($data, 'hasChildren'), TRUE, TRUE));
$terms_with_children = array_intersect_key($data, $keys);
$this
->assertCount(2, $terms_with_children, 'Expected 2 terms having children');
foreach ($terms_with_children_expected as $name) {
$term_exists = array_keys(array_column($terms_with_children, 'name'), $name, TRUE);
$this
->assertCount(1, $term_exists, "Expected term '{$name}' to have children.");
}
}
public function testChildren() : void {
$parent = $this->termIds['aaa 1'];
$field_name = 'shs-' . strtr($this->fieldName, [
'_' => '-',
]);
$request_url = "shs-term-data/{$field_name}/{$this->vocabulary->id()}/{$parent}";
$data = $this
->drupalGetJson($request_url);
$this
->assertCount(4, $data, "JSON callback returned 4 result");
}
}