diff --git a/src/Element/HttpQueryString.php b/src/Element/HttpQueryString.php index 75cf05b..cdec590 100644 --- a/src/Element/HttpQueryString.php +++ b/src/Element/HttpQueryString.php @@ -25,7 +25,7 @@ class HttpQueryString implements \ArrayAccess { * @param array $initial * The initial values of the query string. */ - public function __construct($initial) { + public function __construct(array $initial) { $this->query = $initial; } @@ -36,7 +36,7 @@ class HttpQueryString implements \ArrayAccess { * A http query string. */ function __toString() { - return http_build_query($this->query); + return (count($this->query) > 0 ? '?' : '') . http_build_query($this->query); } /** diff --git a/src/Tests/Kernel/VideoEmbedIFrameTest.php b/src/Tests/Kernel/VideoEmbedIFrameTest.php new file mode 100644 index 0000000..8c8a9c1 --- /dev/null +++ b/src/Tests/Kernel/VideoEmbedIFrameTest.php @@ -0,0 +1,97 @@ + [ + [ + '#type' => 'video_embed_iframe', + ], + '', + ], + 'URL' => [ + [ + '#type' => 'video_embed_iframe', + '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw', + ], + '', + ], + 'URL, query' => [ + [ + '#type' => 'video_embed_iframe', + '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw', + '#query' => ['autoplay' => '1'], + ], + '', + ], + 'URL, query, attributes' => [ + [ + '#type' => 'video_embed_iframe', + '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw', + '#query' => ['autoplay' => '1'], + '#attributes' => [ + 'width' => '100', + ], + ], + '', + ], + 'Query' => [ + [ + '#type' => 'video_embed_iframe', + '#query' => ['autoplay' => '1'], + ], + '', + ], + 'Query, attributes' => [ + [ + '#type' => 'video_embed_iframe', + '#query' => ['autoplay' => '1'], + '#attributes' => [ + 'width' => '100', + ], + ], + '', + ], + 'Attributes' => [ + [ + '#type' => 'video_embed_iframe', + '#attributes' => [ + 'width' => '100', + ], + ], + '', + ], + ]; + } + + /** + * Test the video embed iframe renders correctly. + * + * @dataProvider videoEmbedIFrameTestCases + */ + public function testVideoEmbedIFrame($renderable, $markup) { + $this->assertEquals($markup, trim($this->container->get('renderer')->renderRoot($renderable))); + } + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + } + +} diff --git a/templates/video-embed-iframe.html.twig b/templates/video-embed-iframe.html.twig index 8f9b3d9..5792125 100644 --- a/templates/video-embed-iframe.html.twig +++ b/templates/video-embed-iframe.html.twig @@ -1 +1 @@ - + diff --git a/tests/src/Unit/HttpQueryStringTest.php b/tests/src/Unit/HttpQueryStringTest.php new file mode 100644 index 0000000..15bd258 --- /dev/null +++ b/tests/src/Unit/HttpQueryStringTest.php @@ -0,0 +1,51 @@ + [ + [ + 'cat' => 'house', + 'fish' => 'bowl', + ], + '?cat=house&fish=bowl', + ], + 'Boolean value' => [ + [ + 'autoplay' => TRUE, + ], + '?autoplay=1', + ], + 'Empty initial input' => [ + [], + '', + ], + ]; + } + + /** + * @dataProvider httpQueryObjectTestCases + */ + public function testHttpQueryObject($initial, $expected) { + $this->assertEquals((string) new HttpQueryString($initial), $expected); + } +}