|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\islandora_mirador\Kernel; |
| 4 | + |
| 5 | +use Drupal\Core\Form\FormState; |
| 6 | +use Drupal\KernelTests\KernelTestBase; |
| 7 | +use Drupal\islandora_mirador\Form\MiradorConfigForm; |
| 8 | + |
| 9 | +/** |
| 10 | + * Tests the MiradorConfigForm validation. |
| 11 | + * |
| 12 | + * @group islandora_mirador |
| 13 | + */ |
| 14 | +class MiradorConfigFormTest extends KernelTestBase { |
| 15 | + |
| 16 | + /** |
| 17 | + * {@inheritdoc} |
| 18 | + */ |
| 19 | + protected static $modules = [ |
| 20 | + 'islandora_mirador', |
| 21 | + 'system', |
| 22 | + ]; |
| 23 | + |
| 24 | + /** |
| 25 | + * {@inheritdoc} |
| 26 | + */ |
| 27 | + protected function setUp(): void { |
| 28 | + parent::setUp(); |
| 29 | + $this->installConfig(['islandora_mirador']); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Returns a form object with valid base values for theme fields. |
| 34 | + */ |
| 35 | + protected function validValues(): array { |
| 36 | + return [ |
| 37 | + 'mirador_selected_theme' => 'light', |
| 38 | + 'mirador_theme_light_primary' => '#1967d2', |
| 39 | + 'mirador_theme_light_secondary' => '#1967d2', |
| 40 | + 'mirador_theme_dark_primary' => '#4db6ac', |
| 41 | + 'mirador_theme_dark_secondary' => '#4db6ac', |
| 42 | + ]; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Runs validateForm on a MiradorConfigForm with the given values. |
| 47 | + */ |
| 48 | + protected function validateWithValues(array $values): FormState { |
| 49 | + $form_object = MiradorConfigForm::create($this->container); |
| 50 | + $form_state = new FormState(); |
| 51 | + $form_state->setValues($values); |
| 52 | + $form = []; |
| 53 | + $form_object->validateForm($form, $form_state); |
| 54 | + return $form_state; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Tests that valid theme and colors pass validation. |
| 59 | + */ |
| 60 | + public function testValidLightTheme(): void { |
| 61 | + $form_state = $this->validateWithValues($this->validValues()); |
| 62 | + $this->assertFalse($form_state->hasAnyErrors()); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Tests that dark theme is also valid. |
| 67 | + */ |
| 68 | + public function testValidDarkTheme(): void { |
| 69 | + $values = $this->validValues(); |
| 70 | + $values['mirador_selected_theme'] = 'dark'; |
| 71 | + $form_state = $this->validateWithValues($values); |
| 72 | + $this->assertFalse($form_state->hasAnyErrors()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Tests that the system theme option is valid. |
| 77 | + */ |
| 78 | + public function testValidSystemTheme(): void { |
| 79 | + $values = $this->validValues(); |
| 80 | + $values['mirador_selected_theme'] = 'system'; |
| 81 | + $form_state = $this->validateWithValues($values); |
| 82 | + $this->assertFalse($form_state->hasAnyErrors()); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Tests that a 3-digit hex color is valid. |
| 87 | + */ |
| 88 | + public function testValidShortHexColor(): void { |
| 89 | + $values = $this->validValues(); |
| 90 | + $values['mirador_theme_light_primary'] = '#fff'; |
| 91 | + $form_state = $this->validateWithValues($values); |
| 92 | + $this->assertFalse($form_state->hasAnyErrors()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Tests that an invalid theme value fails validation. |
| 97 | + */ |
| 98 | + public function testInvalidTheme(): void { |
| 99 | + $values = $this->validValues(); |
| 100 | + $values['mirador_selected_theme'] = 'blue'; |
| 101 | + $form_state = $this->validateWithValues($values); |
| 102 | + $this->assertTrue($form_state->hasAnyErrors()); |
| 103 | + $errors = $form_state->getErrors(); |
| 104 | + $this->assertArrayHasKey('mirador_selected_theme', $errors); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Tests that a color without a leading # fails validation. |
| 109 | + */ |
| 110 | + public function testInvalidColorMissingHash(): void { |
| 111 | + $values = $this->validValues(); |
| 112 | + $values['mirador_theme_light_primary'] = '1967d2'; |
| 113 | + $form_state = $this->validateWithValues($values); |
| 114 | + $this->assertTrue($form_state->hasAnyErrors()); |
| 115 | + $errors = $form_state->getErrors(); |
| 116 | + $this->assertArrayHasKey('mirador_theme_light_primary', $errors); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Tests that a color with invalid hex characters fails validation. |
| 121 | + */ |
| 122 | + public function testInvalidColorBadCharacters(): void { |
| 123 | + $values = $this->validValues(); |
| 124 | + $values['mirador_theme_dark_secondary'] = '#zzzzzz'; |
| 125 | + $form_state = $this->validateWithValues($values); |
| 126 | + $this->assertTrue($form_state->hasAnyErrors()); |
| 127 | + $errors = $form_state->getErrors(); |
| 128 | + $this->assertArrayHasKey('mirador_theme_dark_secondary', $errors); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Tests that a color with wrong length fails validation. |
| 133 | + */ |
| 134 | + public function testInvalidColorWrongLength(): void { |
| 135 | + $values = $this->validValues(); |
| 136 | + $values['mirador_theme_dark_primary'] = '#12345'; |
| 137 | + $form_state = $this->validateWithValues($values); |
| 138 | + $this->assertTrue($form_state->hasAnyErrors()); |
| 139 | + $errors = $form_state->getErrors(); |
| 140 | + $this->assertArrayHasKey('mirador_theme_dark_primary', $errors); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Tests that multiple invalid fields each produce their own error. |
| 145 | + */ |
| 146 | + public function testMultipleInvalidFields(): void { |
| 147 | + $values = $this->validValues(); |
| 148 | + $values['mirador_selected_theme'] = 'invalid'; |
| 149 | + $values['mirador_theme_light_secondary'] = 'notacolor'; |
| 150 | + $form_state = $this->validateWithValues($values); |
| 151 | + $errors = $form_state->getErrors(); |
| 152 | + $this->assertArrayHasKey('mirador_selected_theme', $errors); |
| 153 | + $this->assertArrayHasKey('mirador_theme_light_secondary', $errors); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Tests that config defaults are set correctly on fresh install. |
| 158 | + */ |
| 159 | + public function testConfigDefaults(): void { |
| 160 | + $config = $this->config('islandora_mirador.settings'); |
| 161 | + $this->assertEquals('light', $config->get('mirador_selected_theme')); |
| 162 | + $this->assertEquals('#1967d2', $config->get('mirador_theme_light_primary')); |
| 163 | + $this->assertEquals('#1967d2', $config->get('mirador_theme_light_secondary')); |
| 164 | + $this->assertEquals('#4db6ac', $config->get('mirador_theme_dark_primary')); |
| 165 | + $this->assertEquals('#4db6ac', $config->get('mirador_theme_dark_secondary')); |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments