Added a test for basic counting accuracy

This commit is contained in:
aaron.axvig committed 2026-01-24 21:44:29 -06:00
1 parent ae7961ee07
commit 782cd300da
3 files changed
+186 -59

No files matched your search

@@ -0,0 +1,155 @@
<?php
namespace Drupal\Tests\archive_tree\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\node\Entity\Node;
use Drupal\block\Entity\Block;
/**
* Tests the ArchiveTreeBlock logic with mock data.
*
* @group archive_tree
*/
#[\Drupal\KernelTests\RunTestsInSeparateProcesses]
class ArchiveTreeBlockKernelTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'user',
'node',
'block',
'archive_tree',
];
protected function setUp(): void {
parent::setUp();
// Ensure required entity schemas and config are installed.
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installConfig(['node']);
// Create four content types.
NodeType::create(['type' => 'article', 'name' => 'Article'])->save();
NodeType::create(['type' => 'book', 'name' => 'Book'])->save();
NodeType::create(['type' => 'essay', 'name' => 'Essay'])->save();
NodeType::create(['type' => 'poem', 'name' => 'Poem'])->save();
}
/**
* Test archive tree block output with mock nodes.
*/
public function testArchiveTreeBlockOutput() {
// Sample nodes test a few scenarios:
// - Year totals add up correctly (2024 with 5, 2025 with 10).
// - Skipped year (2023) doesn't render.
// - One item in 2022 renders correctly (one showing in both month and year).
// - Month totals add up correctly.
// - 2024-01: 2
// - 2024-02: 3
// - 2025-09: 1
// - 2025-10: 4
// - 2025-11: 3
// - 2025-12: 2
// 2022: 1 node (article, Dec)
$this->createNode('article', strtotime('2022-12-25'));
// 2024: 5 nodes
$this->createNode('book', strtotime('2024-01-05'));
$this->createNode('poem', strtotime('2024-01-10'));
$this->createNode('essay', strtotime('2024-02-01'));
$this->createNode('book', strtotime('2024-02-15'));
$this->createNode('poem', strtotime('2024-02-20'));
// 2025: 10 nodes
$this->createNode('article', strtotime('2025-09-01'));
$this->createNode('essay', strtotime('2025-10-01'));
$this->createNode('essay', strtotime('2025-10-10'));
$this->createNode('book', strtotime('2025-10-15'));
$this->createNode('poem', strtotime('2025-10-20'));
$this->createNode('book', strtotime('2025-11-01'));
$this->createNode('poem', strtotime('2025-11-10'));
$this->createNode('essay', strtotime('2025-11-20'));
$this->createNode('article', strtotime('2025-12-01'));
$this->createNode('book', strtotime('2025-12-10'));
// Place the block with all types selected.
$block = Block::create([
'id' => 'archive_tree_test',
'plugin' => 'archive_tree',
'region' => 'sidebar_first',
'theme' => 'stark',
'settings' => [
'content_types' => ['article', 'book', 'essay', 'poem'],
'expand_years' => TRUE,
],
'visibility' => [],
'weight' => 0,
'status' => 1,
]);
$block->save();
// Render the block.
$block_plugin = \Drupal::service('plugin.manager.block')->createInstance('archive_tree', [
'content_types' => ['article', 'book', 'essay', 'poem'],
'expand_years' => TRUE,
]);
$build = $block_plugin->build();
$output = \Drupal::service('renderer')->renderRoot($build);
// Assert year totals.
$this->assertStringContainsString('2022', $output); // 1 node in 2022
$this->assertStringContainsString('2024', $output); // 5 nodes in 2024
$this->assertStringContainsString('2025', $output); // 10 nodes in 2025
$this->assertStringNotContainsString('2023', $output); // 2023 skipped
// Parse the output to check month counts under the correct year.
$dom = new \DOMDocument();
@$dom->loadHTML('<?xml encoding="utf-8" ?>' . $output);
$xpath = new \DOMXPath($dom);
// Helper to get month/count pairs under a given year.
$getMonthsForYear = function($year) use ($xpath) {
foreach ($xpath->query('//details') as $details) {
$summary = $xpath->query('summary', $details)->item(0);
if ($summary && strpos($summary->textContent, (string)$year) !== false) {
$months = [];
foreach ($xpath->query('.//ul/li', $details) as $li) {
$months[] = trim($li->textContent);
}
return $months;
}
}
return [];
};
// 2024: 01 (2), 02 (3)
$months2024 = $getMonthsForYear(2024);
$this->assertContains('01 (2)', $months2024);
$this->assertContains('02 (3)', $months2024);
// 2025: 09 (1), 10 (4), 11 (3), 12 (2)
$months2025 = $getMonthsForYear(2025);
$this->assertContains('09 (1)', $months2025);
$this->assertContains('10 (4)', $months2025);
$this->assertContains('11 (3)', $months2025);
$this->assertContains('12 (2)', $months2025);
// 2022: 12 (1)
$months2022 = $getMonthsForYear(2022);
$this->assertContains('12 (1)', $months2022);
}
/**
* Helper to create a published node of a type and created date.
*/
protected function createNode($type, $created) {
$node = Node::create([
'type' => $type,
'title' => $this->randomString(),
'status' => 1,
'created' => $created,
]);
$node->save();
return $node;
}
}