diff --git a/tests/src/Kernel/ArchiveTreeBlockKernelTest.php b/tests/src/Kernel/ArchiveTreeBlockKernelTest.php index 7f177b1..c9d293c 100644 --- a/tests/src/Kernel/ArchiveTreeBlockKernelTest.php +++ b/tests/src/Kernel/ArchiveTreeBlockKernelTest.php @@ -139,6 +139,97 @@ class ArchiveTreeBlockKernelTest extends KernelTestBase { $this->assertContains('12 (1)', $months2022); } + /** + * Test archive tree block output with only two content types enabled. + */ + public function testArchiveTreeBlockWithTwoContentTypes() { + $this->createNode('article', strtotime('2022-12-25')); + $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')); + $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 only 'article' and 'book' enabled. + $block = Block::create([ + 'id' => 'archive_tree_test_2', + 'plugin' => 'archive_tree', + 'region' => 'sidebar_first', + 'theme' => 'stark', + 'settings' => [ + 'content_types' => ['article', 'book'], + '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'], + 'expand_years' => TRUE, + ]); + $build = $block_plugin->build(); + $output = \Drupal::service('renderer')->renderRoot($build); + + // Assert that only 'article' and 'book' nodes are counted. + // 2022: 1 article (Dec) + // 2024: 2 books (Jan, Feb) + // 2025: 2 articles (Sep, Dec), 3 books (Oct, Nov, Dec) + $dom = new \DOMDocument(); + @$dom->loadHTML('' . $output); + $xpath = new \DOMXPath($dom); + $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 []; + }; + + // 2022: 12 (1) + $months2022 = $getMonthsForYear(2022); + $this->assertContains('12 (1)', $months2022); + + // 2024: 01 (1), 02 (1) + $months2024 = $getMonthsForYear(2024); + $this->assertContains('01 (1)', $months2024); + $this->assertContains('02 (1)', $months2024); + + // 2025: 09 (1), 10 (1), 11 (1), 12 (2) + $months2025 = $getMonthsForYear(2025); + $this->assertContains('09 (1)', $months2025); // article + $this->assertContains('10 (1)', $months2025); // book + $this->assertContains('11 (1)', $months2025); // book + $this->assertContains('12 (2)', $months2025); // article + book + + // Ensure months for excluded types are not present. + // (e.g., 2024-01 should not have 2, only 1 for book, not poem) + $this->assertNotContains('01 (2)', $months2024); + $this->assertNotContains('02 (3)', $months2024); + $this->assertNotContains('10 (4)', $months2025); + $this->assertNotContains('11 (3)', $months2025); + $this->assertNotContains('12 (1)', $months2025); // should be 2 + } + /** * Helper to create a published node of a type and created date. */ @@ -153,3 +244,4 @@ class ArchiveTreeBlockKernelTest extends KernelTestBase { return $node; } } +