Skip to content

Commit

Permalink
Update modifier and write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martyf committed Dec 17, 2024
1 parent c0318e9 commit b37ae05
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3106,6 +3106,10 @@ public function embedUrl($url)
$url = str_replace('//youtube-nocookie.com', '//www.youtube-nocookie.com', $url);
}

if (Str::contains($url, '&') && ! Str::contains($url, '?')) {
$url = Str::replaceFirst('&', '?', $url);
}

return $url;
}

Expand Down Expand Up @@ -3135,6 +3139,10 @@ public function trackableEmbedUrl($url)
$url = str_replace('watch?v=', 'embed/', $url);
}

if (Str::contains($url, '&') && ! Str::contains($url, '?')) {
$url = Str::replaceFirst('&', '?', $url);
}

return $url;
}

Expand Down
31 changes: 30 additions & 1 deletion tests/Modifiers/EmbedUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,36 @@ public function it_transforms_youtube_urls()
);
}

public function embed($url)
#[Test]
public function it_ensures_url_with_query_parameters_are_valid()
{
$embedUrl = 'https://www.youtube-nocookie.com/embed/s72r_wu_NVY?pp=player_params';

$this->assertEquals(
$embedUrl,
$this->embed('https://www.youtube.com/watch?v=s72r_wu_NVY&pp=player_params'),
'It transforms the youtube video link with additional query string params'
);
$this->assertEquals(
$embedUrl,
$this->embed('https://youtu.be/s72r_wu_NVY?pp=player_params'),
'It transforms shortened youtube video sharing links with additional query string params'
);

$this->assertEquals(
'https://www.youtube-nocookie.com/embed/s72r_wu_NVY?start=559&pp=player_params',
$this->embed('https://youtu.be/s72r_wu_NVY?t=559&pp=player_params'),
'It transforms the start time parameter of shortened sharing links with additional query string params'
);

$this->assertEquals(
'https://www.youtube-nocookie.com/embed/hyJ7CBs_2RQ?start=2&pp=player_params',
$this->embed('https://www.youtube.com/watch?v=hyJ7CBs_2RQ&t=2&pp=player_params'),
'It transforms the start time parameter of full youtube links with additional query string params'
);
}

public function embed($url)
{
return Modify::value($url)->embedUrl()->fetch();
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Modifiers/TrackableEmbedUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ public function it_transforms_youtube_urls()
);
}

public function it_ensures_url_with_query_parameters_are_valid()
{
$embedUrl = 'https://www.youtube-nocookie.com/embed/s72r_wu_NVY?pp=player_params';

$this->assertEquals(
$embedUrl,
$this->embed('https://www.youtube.com/watch?v=s72r_wu_NVY&pp=player_params'),
'It transforms the youtube video link with additional query string params'
);
$this->assertEquals(
$embedUrl,
$this->embed('https://youtu.be/s72r_wu_NVY?pp=player_params'),
'It transforms shortened youtube video sharing links with additional query string params'
);

$this->assertEquals(
'https://www.youtube-nocookie.com/embed/s72r_wu_NVY?start=559&pp=player_params',
$this->embed('https://youtu.be/s72r_wu_NVY?t=559&pp=player_params'),
'It transforms the start time parameter of shortened sharing links with additional query string params'
);

$this->assertEquals(
'https://www.youtube-nocookie.com/embed/hyJ7CBs_2RQ?start=2&pp=player_params',
$this->embed('https://www.youtube.com/watch?v=hyJ7CBs_2RQ&t=2&pp=player_params'),
'It transforms the start time parameter of full youtube links with additional query string params'
);
}

public function embed($url)
{
return Modify::value($url)->trackableEmbedUrl()->fetch();
Expand Down

0 comments on commit b37ae05

Please sign in to comment.