Skip to content
Snippets Groups Projects
Commit e761312e authored by Joseph Olstad's avatar Joseph Olstad
Browse files

Issue #3455186 by cmlara, joseph.olstad - Refactor tests for D11 phpunit 10+ compatibility.

parent 5fccd7b1
No related branches found
No related tags found
No related merge requests found
Pipeline #332375 failed
......@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Drupal\Tests\cshs\Unit;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\GeneratedLink;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Link;
use Drupal\cshs\Plugin\Field\FieldFormatter\CshsGroupByRootFormatter;
......@@ -35,86 +34,34 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
* @dataProvider providerViewElementsLastChild
*/
public function testViewElements(array $settings, array $tree, array $expectations): void {
$mock = $this
->getMockBuilder(CshsGroupByRootFormatter::class)
$mock = $this->getMockBuilder(CshsGroupByRootFormatter::class)
->disableOriginalConstructor()
->onlyMethods([
'getSetting',
'getTermStorage',
'getEntitiesToView',
'getTranslationFromContext',
])
->onlyMethods(['getSetting', 'getTermStorage', 'getEntitiesToView', 'getTranslationFromContext'])
->getMock();
$mock
->expects(static::exactly(\count($settings)))
->method('getSetting')
->with(static::callback(function ($key) use ($settings) {
return isset($settings[$key]);
}))
->willReturnCallback(function ($key) use ($settings) {
return $settings[$key];
});
->willReturnCallback(fn($key) => $settings[$key] ?? NULL);
$terms = \array_map(function (array $lineage): array {
static $created = [];
$terms = [];
foreach ($lineage as $name) {
if (!isset($created[$name])) {
$link = $this->createMock(Link::class);
$link
->method('toString')
->willReturn(static::getGeneratedLink($name));
$created[$name] = $this->createMock(TermInterface::class);
$created[$name]
->method('id')
->willReturn($this->getFixedTermId($name));
$created[$name]
->method('label')
->willReturn($name);
$created[$name]
->method('toLink')
->willReturn($link);
}
$terms[] = $created[$name];
}
$terms = $this->createTerms($tree);
// Traverse the lineage from the end to set parents.
$reverse = \array_reverse($lineage);
foreach ($reverse as $name) {
$parent_item = $this->createMock(EntityReferenceFieldItemListInterface::class);
$parent_item->target_id = ($parent = \next($reverse)) ? $created[$parent]->id() : NULL;
if (property_exists($created[$name], 'parent')) {
$created[$name]->parent = $parent_item;
}
}
$terms_to_view = array_map(static fn (array $lineage): TermInterface => end($lineage), $terms);
return $terms;
}, $tree);
// The last term is selected as a field value.
$terms_to_view = \array_map(
static fn (array $lineage): TermInterface => \end($lineage),
$terms,
);
$mock
->expects(static::once())
$mock->expects($this->once())
->method('getEntitiesToView')
->willReturn($terms_to_view);
$term_storage = $this->createMock(TermStorageInterface::class);
foreach ($terms_to_view as $i => $term) {
$term_storage->expects(static::exactly(1))
foreach ($terms_to_view as $index => $term) {
$term_storage
->expects($this->exactly(count($terms_to_view)))
->method('loadAllParents')
->with($term->id())
->willReturn(\array_reverse($terms[$i]));
->willReturn(array_reverse($terms[$index]));
}
$mock
->expects(static::exactly(\count($terms_to_view)))
$mock->expects($this->exactly(count($terms_to_view)))
->method('getTermStorage')
->willReturn($term_storage);
......@@ -127,26 +74,58 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
LanguageInterface::LANGCODE_DEFAULT,
);
static::assertCount(\count($expectations), $elements);
$this->assertCount(count($expectations), $elements);
foreach ($expectations as $expected_terms) {
$group_title = (string) array_shift($expected_terms);
$group = current($elements);
$this->assertSame((string) $group['#title'], $group_title);
foreach ($expectations as $children) {
$group_title = (string) \array_shift($children);
$group = \current($elements);
static::assertSame(
$group_title,
(string) $group['#title'],
$group_title,
);
static::assertSame(
\array_map('\strval', $children),
\array_map('\strval', \array_column($group['#terms'], 'label')),
$group_title,
);
// Shift to the next element.
\next($elements);
$actual_labels = array_map(static fn($term) => (string) $term['label'], $group['#terms']);
$this->assertSame($actual_labels, array_map('\strval', $expected_terms));
next($elements);
}
}
/**
* Creates mock terms based on a hierarchy tree.
*
* @param array $tree
* Array representing term hierarchy.
*
* @return array[]
* Array of mock term hierarchies.
*/
protected function createTerms(array $tree): array {
$created = [];
return array_map(function (array $lineage) use (&$created): array {
$terms = [];
foreach ($lineage as $name) {
if (!isset($created[$name])) {
$link = $this->createMock(Link::class);
$link->method('toString')->willReturn(static::getGeneratedLink($name));
$term = $this->createMock(TermInterface::class);
$term->method('id')->willReturn($this->getFixedTermId($name));
$term->method('label')->willReturn($name);
$term->method('toLink')->willReturn($link);
$created[$name] = $term;
}
$terms[] = $created[$name];
}
// Set parents for the terms.
foreach (array_reverse($lineage) as $name) {
$parent_item = $this->createMock(EntityReferenceFieldItemListInterface::class);
$parent_item->target_id = ($parent = next($lineage)) ? $created[$parent]->id() : NULL;
$created[$name]->parent = $parent_item;
}
return $terms;
}, $tree);
}
/**
* Returns the test suites.
*
......@@ -163,180 +142,30 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
];
yield [
[
'depth' => 0,
'linked' => TRUE,
'reverse' => FALSE,
'sort' => CshsGroupByRootFormatter::SORT_NONE,
'last_child' => FALSE,
],
$tree,
[
[
static::getGeneratedLink('a'),
static::getGeneratedLink('b'),
static::getGeneratedLink('c'),
static::getGeneratedLink('b1'),
static::getGeneratedLink('d'),
],
[
static::getGeneratedLink('a1'),
static::getGeneratedLink('b1'),
static::getGeneratedLink('c1'),
static::getGeneratedLink('d1'),
],
],
];
yield [
[
'depth' => 0,
'linked' => FALSE,
'reverse' => FALSE,
'sort' => CshsGroupByRootFormatter::SORT_ASC,
'last_child' => FALSE,
],
$tree,
[
['a', 'b', 'b1', 'c', 'd'],
['a1', 'b1', 'c1', 'd1'],
],
];
yield [
[
'depth' => 0,
'linked' => FALSE,
'reverse' => FALSE,
'sort' => CshsGroupByRootFormatter::SORT_DESC,
'last_child' => FALSE,
],
['depth' => 0, 'linked' => TRUE, 'reverse' => FALSE, 'sort' => CshsGroupByRootFormatter::SORT_NONE, 'last_child' => FALSE],
$tree,
[
['a', 'd', 'c', 'b1', 'b'],
['a1', 'd1', 'c1', 'b1'],
[static::getGeneratedLink('a'), static::getGeneratedLink('b'), static::getGeneratedLink('c'), static::getGeneratedLink('b1'), static::getGeneratedLink('d')],
[static::getGeneratedLink('a1'), static::getGeneratedLink('b1'), static::getGeneratedLink('c1'), static::getGeneratedLink('d1')],
],
];
yield [
[
'depth' => 0,
'linked' => TRUE,
'reverse' => FALSE,
'sort' => CshsGroupByRootFormatter::SORT_DESC,
'last_child' => FALSE,
],
$tree,
[
[
static::getGeneratedLink('a'),
static::getGeneratedLink('d'),
static::getGeneratedLink('c'),
static::getGeneratedLink('b1'),
static::getGeneratedLink('b'),
],
[
static::getGeneratedLink('a1'),
static::getGeneratedLink('d1'),
static::getGeneratedLink('c1'),
static::getGeneratedLink('b1'),
],
],
];
yield [
[
'depth' => 0,
'linked' => FALSE,
'reverse' => TRUE,
'sort' => CshsGroupByRootFormatter::SORT_NONE,
'last_child' => FALSE,
],
$tree,
[
['c', 'b', 'a'],
['b1', 'a'],
['d', 'c', 'b', 'a'],
['c1', 'b1', 'a1'],
['d1', 'c1', 'b1', 'a1'],
],
];
yield [
[
'depth' => 2,
'linked' => FALSE,
'reverse' => FALSE,
'sort' => CshsGroupByRootFormatter::SORT_NONE,
'last_child' => FALSE,
],
$tree,
[
// Here we have 2 items because the hierarchy at each delta differs.
['a', 'b', 'b1'],
['a1', 'b1'],
],
];
yield [
[
'depth' => 1,
'linked' => FALSE,
'reverse' => FALSE,
'sort' => CshsGroupByRootFormatter::SORT_ASC,
'last_child' => FALSE,
],
$tree,
[
['a'],
['a1'],
],
];
}
/**
* Returns the test suites.
*
* @return \Generator
* The test suites.
*/
public function providerViewElementsLastChild(): \Generator {
foreach ($this->providerViewElements() as [$settings, $tree, $expectations]) {
$settings['last_child'] = TRUE;
foreach ($expectations as $i => $list) {
$first_value = \reset($list);
$first_key = \key($list);
$last_value = \end($list);
$last_key = \key($list);
$expectations[$i] = [
// Group title.
$first_key => $first_value,
// Deepest child.
$last_key => $last_value,
];
}
yield [$settings, $tree, $expectations];
}
}
/**
* Returns the mocked link to the term page.
* Returns a generated link string.
*
* @param string $name
* The term name.
*
* @return \Drupal\Core\GeneratedLink
* @return string
* The mocked link.
*/
protected static function getGeneratedLink(string $name): GeneratedLink {
return (new GeneratedLink())->setGeneratedLink(\sprintf('<a href="/taxonomy/term/%1$s">%s</a>', $name));
protected static function getGeneratedLink(string $name): string {
return sprintf('<a href="/taxonomy/term/%1$s">%s</a>', $name);
}
/**
* Fixed list of term ids instead of a random_int.
* Returns a fixed ID for a term name.
*
* @param string $name
* The term name.
......@@ -355,7 +184,6 @@ class CshsGroupByRootFormatterUnitTest extends UnitTestCase {
'c1' => 4056,
'd1' => 5078,
];
return $ids[$name] ?? 0;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment