|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Drupal\Tests\psulib_base_helper\Unit\Hook; |
| 6 | + |
| 7 | +use Drupal\Core\Config\ConfigFactoryInterface; |
| 8 | +use Drupal\Core\Config\ImmutableConfig; |
| 9 | +use Drupal\psulib_base_helper\Hook\ThemeHooks; |
| 10 | +use Drupal\psulib_base_helper\PsuFederatedDataFetcher; |
| 11 | +use Drupal\Tests\UnitTestCase; |
| 12 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 13 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 14 | +use PHPUnit\Framework\Attributes\Group; |
| 15 | +use PHPUnit\Framework\Attributes\Test; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests for ThemeHooks. |
| 19 | + */ |
| 20 | +#[Group('psulib_base_helper')] |
| 21 | +#[CoversClass(ThemeHooks::class)] |
| 22 | +class ThemeHooksTest extends UnitTestCase { |
| 23 | + |
| 24 | + /** |
| 25 | + * The mocked data fetcher. |
| 26 | + * |
| 27 | + * @var \Drupal\psulib_base_helper\PsuFederatedDataFetcher|\PHPUnit\Framework\MockObject\MockObject |
| 28 | + */ |
| 29 | + protected $dataFetcher; |
| 30 | + |
| 31 | + /** |
| 32 | + * The mocked config factory. |
| 33 | + * |
| 34 | + * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit\Framework\MockObject\MockObject |
| 35 | + */ |
| 36 | + protected $configFactory; |
| 37 | + |
| 38 | + /** |
| 39 | + * The mocked config. |
| 40 | + * |
| 41 | + * @var \Drupal\Core\Config\ImmutableConfig|\PHPUnit\Framework\MockObject\MockObject |
| 42 | + */ |
| 43 | + protected $config; |
| 44 | + |
| 45 | + /** |
| 46 | + * The ThemeHooks instance. |
| 47 | + * |
| 48 | + * @var \Drupal\psulib_base_helper\Hook\ThemeHooks |
| 49 | + */ |
| 50 | + protected ThemeHooks $hooks; |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + protected function setUp(): void { |
| 56 | + parent::setUp(); |
| 57 | + |
| 58 | + $this->dataFetcher = $this->createMock(PsuFederatedDataFetcher::class); |
| 59 | + $this->configFactory = $this->createMock(ConfigFactoryInterface::class); |
| 60 | + $this->config = $this->createMock(ImmutableConfig::class); |
| 61 | + |
| 62 | + $this->configFactory->method('get') |
| 63 | + ->with('psulib_base.settings') |
| 64 | + ->willReturn($this->config); |
| 65 | + |
| 66 | + $this->hooks = new ThemeHooks( |
| 67 | + $this->dataFetcher, |
| 68 | + $this->configFactory |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Test that the federated footer render array is added when enabled. |
| 74 | + */ |
| 75 | + #[Test] |
| 76 | + public function testFederatedFooterAddedWhenSettingsAndDataPresent(): void { |
| 77 | + $this->config->method('get') |
| 78 | + ->with('show_federated_footer') |
| 79 | + ->willReturn(TRUE); |
| 80 | + |
| 81 | + $expectedLinks = [ |
| 82 | + ['title' => 'Libraries', 'url' => 'https://example.org/libraries'], |
| 83 | + ['title' => 'Research', 'url' => 'https://example.org/research'], |
| 84 | + ]; |
| 85 | + |
| 86 | + $this->dataFetcher->expects($this->once()) |
| 87 | + ->method('getFederatedData') |
| 88 | + ->with('brandFooter') |
| 89 | + ->willReturn([ |
| 90 | + 'linkContentCollection' => [ |
| 91 | + 'items' => $expectedLinks, |
| 92 | + ], |
| 93 | + ]); |
| 94 | + |
| 95 | + $variables = ['page' => []]; |
| 96 | + |
| 97 | + $this->hooks->preprocessPage($variables); |
| 98 | + |
| 99 | + $this->assertArrayHasKey('federated_footer', $variables['page']); |
| 100 | + $this->assertSame('component', $variables['page']['federated_footer']['#type']); |
| 101 | + $this->assertSame('psulib_base:federated_footer', $variables['page']['federated_footer']['#component']); |
| 102 | + $this->assertSame( |
| 103 | + ['links' => $expectedLinks], |
| 104 | + $variables['page']['federated_footer']['#props'] |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Test that the federated footer is not added when disabled or missing data. |
| 110 | + * |
| 111 | + * @param bool|null $showSetting |
| 112 | + * The show_federated_footer setting value. |
| 113 | + * @param array $data |
| 114 | + * The data returned by the federated data fetcher. |
| 115 | + * @param bool $expectFetcherCall |
| 116 | + * Whether the data fetcher should be called. |
| 117 | + */ |
| 118 | + #[Test] |
| 119 | + #[DataProvider('federatedFooterNotAddedProvider')] |
| 120 | + public function testFederatedFooterNotAddedWhenNoDataOrDisabled(?bool $showSetting, array $data, bool $expectFetcherCall): void { |
| 121 | + $this->config->method('get') |
| 122 | + ->with('show_federated_footer') |
| 123 | + ->willReturn($showSetting); |
| 124 | + |
| 125 | + if ($expectFetcherCall) { |
| 126 | + $this->dataFetcher->expects($this->once()) |
| 127 | + ->method('getFederatedData') |
| 128 | + ->with('brandFooter') |
| 129 | + ->willReturn($data); |
| 130 | + } |
| 131 | + else { |
| 132 | + $this->dataFetcher->expects($this->never()) |
| 133 | + ->method('getFederatedData'); |
| 134 | + } |
| 135 | + |
| 136 | + $variables = ['page' => []]; |
| 137 | + |
| 138 | + $this->hooks->preprocessPage($variables); |
| 139 | + |
| 140 | + $this->assertArrayNotHasKey('federated_footer', $variables['page']); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Data provider for disabled or empty federated footer scenarios. |
| 145 | + * |
| 146 | + * @return array |
| 147 | + * The data sets for the test. |
| 148 | + */ |
| 149 | + public static function federatedFooterNotAddedProvider(): array { |
| 150 | + $validData = [ |
| 151 | + 'linkContentCollection' => [ |
| 152 | + 'items' => [ |
| 153 | + ['label' => 'Libraries', 'url' => 'https://example.org/libraries'], |
| 154 | + ], |
| 155 | + ], |
| 156 | + ]; |
| 157 | + |
| 158 | + return [ |
| 159 | + 'disabled via setting' => [FALSE, $validData, FALSE], |
| 160 | + 'missing setting' => [NULL, $validData, FALSE], |
| 161 | + 'no data' => [TRUE, [], TRUE], |
| 162 | + 'missing link collection' => [TRUE, ['linkContentCollection' => []], TRUE], |
| 163 | + 'empty items' => [TRUE, ['linkContentCollection' => ['items' => []]], TRUE], |
| 164 | + ]; |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments