|
| 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\ExternalWidget; |
| 7 | +use Drupal\Tests\ghi_base_objects\Traits\BaseObjectTestTrait; |
| 8 | + |
| 9 | +/** |
| 10 | + * Tests the external widget block plugin. |
| 11 | + * |
| 12 | + * @group ghi_blocks |
| 13 | + */ |
| 14 | +class ExternalWidgetBlockTest extends BlockKernelTestBase { |
| 15 | + |
| 16 | + use BaseObjectTestTrait; |
| 17 | + |
| 18 | + /** |
| 19 | + * {@inheritdoc} |
| 20 | + */ |
| 21 | + protected function setUp(): void { |
| 22 | + parent::setUp(); |
| 23 | + |
| 24 | + $this->installEntitySchema('base_object'); |
| 25 | + $this->createBaseObjectType([ |
| 26 | + 'id' => 'plan', |
| 27 | + ]); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Tests the link bock. |
| 32 | + */ |
| 33 | + public function testBlockProperties() { |
| 34 | + $widget = $this->buildWidgetConfiguration(); |
| 35 | + $plugin = $this->getBlockPlugin([$widget]); |
| 36 | + $this->assertInstanceOf(ExternalWidget::class, $plugin); |
| 37 | + |
| 38 | + // Make the private method callable. |
| 39 | + $method = (new \ReflectionClass(ExternalWidget::class))->getMethod('getAllowedHosts'); |
| 40 | + $allowed_hosts = $method->invoke($plugin); |
| 41 | + $this->assertCount(4, $allowed_hosts); |
| 42 | + $this->assertArrayHasKey('humdata.org', $allowed_hosts); |
| 43 | + $this->assertArrayHasKey('powerbi.com', $allowed_hosts); |
| 44 | + $this->assertArrayHasKey('tableau.com', $allowed_hosts); |
| 45 | + $this->assertArrayHasKey('experience.arcgis.com', $allowed_hosts); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Tests the block build. |
| 50 | + */ |
| 51 | + public function testBlockBuild() { |
| 52 | + $widgets = [ |
| 53 | + $this->buildWidgetConfiguration('https://app.powerbi.com/view'), |
| 54 | + ]; |
| 55 | + $plugin = $this->getBlockPlugin($widgets); |
| 56 | + |
| 57 | + $build = $plugin->buildContent(); |
| 58 | + $this->assertArrayHasKey(0, $build); |
| 59 | + $this->assertArrayNotHasKey(1, $build); |
| 60 | + |
| 61 | + // Add an additional empty widget. |
| 62 | + $widgets = [ |
| 63 | + $this->buildWidgetConfiguration('https://app.powerbi.com/view'), |
| 64 | + $this->buildWidgetConfiguration(), |
| 65 | + ]; |
| 66 | + $plugin = $this->getBlockPlugin($widgets); |
| 67 | + |
| 68 | + $build = $plugin->buildContent(); |
| 69 | + $this->assertArrayHasKey(0, $build); |
| 70 | + $this->assertArrayNotHasKey(1, $build); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Tests the block build with no widgets. |
| 75 | + */ |
| 76 | + public function testBlockBuildNoWidgets() { |
| 77 | + $widget = $this->buildWidgetConfiguration(); |
| 78 | + $plugin = $this->getBlockPlugin([$widget]); |
| 79 | + |
| 80 | + $build = $plugin->buildContent(); |
| 81 | + $this->assertNull($build); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Tests the link block forms. |
| 86 | + */ |
| 87 | + public function testBlockForms() { |
| 88 | + $plugin = $this->getBlockPlugin(); |
| 89 | + |
| 90 | + $form_state = new FormState(); |
| 91 | + $form_state->set('block', $plugin); |
| 92 | + $form = $plugin->getConfigForm(['#parents' => []], $form_state); |
| 93 | + $this->assertArrayHasKey('select_number', $form); |
| 94 | + $this->assertArrayHasKey('widgets', $form); |
| 95 | + |
| 96 | + // Prepare form validation. |
| 97 | + $form['widgets'][1]['widget_url']['#parents'] = ['container', 'widgets', 1]; |
| 98 | + $form_state->set('current_subform', 'basic'); |
| 99 | + |
| 100 | + // Validate a valid embed code. |
| 101 | + $form_state->setValue(['basic', 'select_number'], 1); |
| 102 | + $form_state->setValue(['basic', 'widgets', 1], $this->buildWidgetConfiguration()); |
| 103 | + $plugin->blockValidate(['container' => $form], $form_state); |
| 104 | + $this->assertNotEmpty($form_state->getErrors()); |
| 105 | + |
| 106 | + $form_state->clearErrors(); |
| 107 | + $form_state->setValue(['basic', 'widgets', 1], $this->buildWidgetConfiguration('https://app.powerbi.com/view')); |
| 108 | + $plugin->blockValidate(['container' => $form], $form_state); |
| 109 | + $this->assertEmpty($form_state->getErrors()); |
| 110 | + |
| 111 | + $form_state->clearErrors(); |
| 112 | + $form_state->setValue(['basic', 'widgets', 1], $this->buildWidgetConfiguration('https://invalid.url/view')); |
| 113 | + $plugin->blockValidate(['container' => $form], $form_state); |
| 114 | + $this->assertNotEmpty($form_state->getErrors()); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Get a link block plugin. |
| 119 | + * |
| 120 | + * @param array $widgets |
| 121 | + * The widget configurations to add to the plugin. |
| 122 | + * |
| 123 | + * @return \Drupal\ghi_blocks\Plugin\Block\Generic\ExternalWidget |
| 124 | + * The block plugin. |
| 125 | + */ |
| 126 | + private function getBlockPlugin($widgets = []) { |
| 127 | + $configuration = [ |
| 128 | + 'select_number' => min(count($widgets), 2), |
| 129 | + 'widgets' => $widgets, |
| 130 | + ]; |
| 131 | + return $this->createBlockPlugin('generic_external_widget', $configuration); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Build a widget configuration. |
| 136 | + * |
| 137 | + * @param string $url |
| 138 | + * The widget url. |
| 139 | + * @param bool $process |
| 140 | + * Whether to process the url. |
| 141 | + * @param bool $skip_validation |
| 142 | + * Whether to skipt the validation. |
| 143 | + * @param string $height |
| 144 | + * The height of the widget. |
| 145 | + * |
| 146 | + * @return array |
| 147 | + * The configuration array for a widget. |
| 148 | + */ |
| 149 | + private function buildWidgetConfiguration($url = '', $process = TRUE, $skip_validation = FALSE, $height = '600px') { |
| 150 | + return [ |
| 151 | + 'widget_url' => $url, |
| 152 | + 'process_widget_url' => $process, |
| 153 | + 'widget_url_skip_validation' => $skip_validation, |
| 154 | + 'widget_height' => $height, |
| 155 | + ]; |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments