Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Add more options to youtube viewhelper and unittest #770

Merged
merged 1 commit into from
Feb 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 49 additions & 16 deletions Classes/ViewHelpers/Media/YoutubeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public function initializeArguments() {
$this->registerArgument('legacyCode', 'boolean', 'Whether to use the legacy flash video code.', FALSE, FALSE);
$this->registerArgument('showRelated', 'boolean', 'Whether to show related videos after playing.', FALSE, FALSE);
$this->registerArgument('extendedPrivacy', 'boolean', 'Whether to use cookie-less video player.', FALSE, TRUE);
$this->registerArgument('hideControl', 'boolean', 'Hide video player\'s control bar.', FALSE, FALSE);
$this->registerArgument('hideInfo', 'boolean', 'Hide video player\'s info bar.', FALSE, FALSE);
$this->registerArgument('playlist', 'string', 'Comma seperated list of video IDs to be played.', FALSE);
$this->registerArgument('loop', 'boolean', 'Play the video in a loop.', FALSE, FALSE);
$this->registerArgument('start', 'integer', 'Start playing after seconds.', FALSE);
$this->registerArgument('end', 'integer', 'Stop playing after seconds.', FALSE);
$this->registerArgument('lightTheme', 'boolean', 'Use the YouTube player\'s light theme.', FALSE, FALSE);
$this->registerArgument('videoQuality', 'string', 'Set the YouTube player\'s video quality (hd1080,hd720,highres,large,medium,small).', FALSE);
}

/**
Expand Down Expand Up @@ -112,25 +120,50 @@ public function render() {
*/
private function getSourceUrl($videoId) {
$src = (boolean) TRUE === $this->arguments['extendedPrivacy'] ? self::YOUTUBE_PRIVACY_BASEURL : self::YOUTUBE_BASEURL;

$params = array();

if (FALSE === (boolean) $this->arguments['showRelated']) {
$params[] = 'rel=0';
}
if (TRUE === (boolean) $this->arguments['autoplay']) {
$params[] = 'autoplay=1';
}
if (TRUE === (boolean) $this->arguments['hideControl']) {
$params[] = 'controls=0';
}
if (TRUE === (boolean) $this->arguments['hideInfo']) {
$params[] = 'showinfo=0';
}
if (FALSE === empty($this->arguments['playlist'])) {
$params[] = 'playlist=' . $this->arguments['playlist'];
}
if (TRUE === (boolean) $this->arguments['loop']) {
$params[] = 'loop=1';
}
if (FALSE === empty($this->arguments['start'])) {
$params[] = 'start=' . $this->arguments['start'];
}
if (FALSE === empty($this->arguments['end'])) {
$params[] = 'end=' . $this->arguments['end'];
}
if (TRUE === (boolean) $this->arguments['lightTheme']) {
$params[] = 'theme=light';
}
if (FALSE === empty($this->arguments['videoQuality'])) {
$params[] = 'vq=' . $this->arguments['videoQuality'];
}

if (FALSE === $this->arguments['legacyCode']) {
$src .= '/embed/'. $videoId;
if (FALSE === (boolean) $this->arguments['showRelated'] || TRUE === (boolean) $this->arguments['autoplay']) {
$src .= '?';
}
if (FALSE === (boolean) $this->arguments['showRelated']) {
$src .= 'rel=0&';
}
if (TRUE === (boolean) $this->arguments['autoplay']) {
$src .= 'autoplay=1';
}
$seperator = '?';
} else {
$src .= '/v/' . $this->arguments['videoId'] . '?version=3';
if (FALSE === (boolean) $this->arguments['showRelated']) {
$src .= '&rel=0';
}
if (TRUE === (boolean) $this->arguments['autoplay']) {
$src .= '&autoplay=1';
}
$src .= '/v/' . $videoId . '?version=3';
$seperator = '&';
}

if (FALSE === empty($params)) {
$src .= $seperator . implode('&', $params);
}

return $src;
Expand Down
31 changes: 31 additions & 0 deletions Tests/Unit/ViewHelpers/Media/YoutubeViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,35 @@
*/
class YoutubeViewHelperTest extends AbstractViewHelperTest {

protected $arguments = array(
'videoId' => '',
'width' => 640,
'height' => 385,
'autoplay' => FALSE,
'legacyCode' => FALSE,
'showRelated' => FALSE,
'extendedPrivacy' => TRUE,
'hideControl' => FALSE,
'hideInfo' => FALSE,
'playlist' => '',
'loop' => FALSE,
'start' => 30,
'end' => '',
'lightTheme' => FALSE,
'videoQuality' => ''
);

/**
* @test
*/
public function compareResult() {
$this->arguments['videoId'] = 'M7lc1UVf-VE';
$this->arguments['hideInfo'] = TRUE;
$this->arguments['start'] = 30;

preg_match('#src="([^"]*)"#', $this->executeViewHelper($this->arguments), $actualSource);
$expectedSource = '//www.youtube-nocookie.com/embed/M7lc1UVf-VE?rel=0&showinfo=0&start=30';

$this->assertSame($expectedSource, $actualSource[1]);
}
}