|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\ghi_blocks\Kernel; |
| 4 | + |
| 5 | +use Drupal\Core\Form\FormState; |
| 6 | +use Drupal\ghi_blocks\Plugin\Block\Generic\Datawrapper; |
| 7 | + |
| 8 | +/** |
| 9 | + * Tests the datawrapper block plugin. |
| 10 | + * |
| 11 | + * @group ghi_blocks |
| 12 | + */ |
| 13 | +class DatawrapperBlockTest extends BlockKernelTestBase { |
| 14 | + |
| 15 | + const EMBED_CODE_VALID = '<iframe src="https://datawrapper.dwcdn.net/CHART_ID"></iframe>'; |
| 16 | + const EMBED_CODE_INVALID = '<iframe src="https://invalid.url/CHART_ID"></iframe>'; |
| 17 | + |
| 18 | + /** |
| 19 | + * Tests the block. |
| 20 | + */ |
| 21 | + public function testBlockProperties() { |
| 22 | + $plugin = $this->getBlockPlugin(); |
| 23 | + $this->assertInstanceOf(Datawrapper::class, $plugin); |
| 24 | + |
| 25 | + $allowed_hosts = $this->callPrivateMethod($plugin, 'getAllowedHosts'); |
| 26 | + $this->assertCount(1, $allowed_hosts); |
| 27 | + $this->assertArrayHasKey('datawrapper.dwcdn.net', $allowed_hosts); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Tests the block build. |
| 32 | + */ |
| 33 | + public function testBlockBuild() { |
| 34 | + $plugin = $this->getBlockPlugin(); |
| 35 | + $build = $plugin->buildContent(); |
| 36 | + $this->assertNull($build); |
| 37 | + |
| 38 | + $plugin = $this->getBlockPlugin(self::EMBED_CODE_VALID); |
| 39 | + $build = $plugin->buildContent(); |
| 40 | + $this->assertArrayHasKey(0, $build); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Tests the block forms. |
| 45 | + */ |
| 46 | + public function testBlockForms() { |
| 47 | + $plugin = $this->getBlockPlugin(); |
| 48 | + |
| 49 | + $form_state = new FormState(); |
| 50 | + $form_state->set('block', $plugin); |
| 51 | + $form = $plugin->getConfigForm(['#parents' => []], $form_state); |
| 52 | + $this->assertArrayHasKey('embed', $form); |
| 53 | + |
| 54 | + // Prepare form validation. |
| 55 | + $form['embed']['#parents'] = ['container']; |
| 56 | + $form_state->set('current_subform', 'basic'); |
| 57 | + |
| 58 | + // Validate a valid embed code. |
| 59 | + $form_state->setValue(['basic', 'embed'], self::EMBED_CODE_VALID); |
| 60 | + $plugin->blockValidate(['container' => $form], $form_state); |
| 61 | + $this->assertEmpty($form_state->getErrors()); |
| 62 | + |
| 63 | + // Validate an invalid embed code. |
| 64 | + $form_state->setValue(['basic', 'embed'], self::EMBED_CODE_INVALID); |
| 65 | + $plugin->blockValidate(['container' => $form], $form_state); |
| 66 | + $this->assertNotEmpty($form_state->getErrors()); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get a block plugin. |
| 71 | + * |
| 72 | + * @param array $embed |
| 73 | + * The embed code for the plugin. |
| 74 | + * |
| 75 | + * @return \Drupal\ghi_blocks\Plugin\Block\Generic\Datawrapper |
| 76 | + * The block plugin. |
| 77 | + */ |
| 78 | + private function getBlockPlugin($embed = '') { |
| 79 | + $configuration = [ |
| 80 | + 'embed' => $embed, |
| 81 | + ]; |
| 82 | + return $this->createBlockPlugin('generic_datawrapper', $configuration); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments