-
-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve query params manipulation (#2638)
- Loading branch information
Showing
6 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Tests\Modifiers; | ||
|
||
use Statamic\Modifiers\Modify; | ||
use Tests\TestCase; | ||
|
||
class AddQueryParamTest extends TestCase | ||
{ | ||
protected $baseUrl = 'https://www.google.com/search'; | ||
protected $queryParam = ['q', 'test']; | ||
|
||
/** @test */ | ||
public function it_adds_a_new_query_param() | ||
{ | ||
$this->assertSame("{$this->baseUrl}?q=", $this->modify($this->baseUrl, ['q'])); | ||
$this->assertSame("{$this->baseUrl}?q=test", $this->modify($this->baseUrl, $this->queryParam)); | ||
$this->assertSame("{$this->baseUrl}?sourceid=chrome&q=test", $this->modify("{$this->baseUrl}?sourceid=chrome", $this->queryParam)); | ||
$this->assertSame("{$this->baseUrl}?q=test#test", $this->modify("{$this->baseUrl}#test", $this->queryParam)); | ||
} | ||
|
||
/** @test */ | ||
public function it_does_nothing_if_no_parameters_are_passed() | ||
{ | ||
$this->assertSame($this->baseUrl, $this->modify($this->baseUrl)); | ||
$this->assertSame("{$this->baseUrl}#test", $this->modify("{$this->baseUrl}#test")); | ||
} | ||
|
||
private function modify(string $url, ?array $queryParam = null) | ||
{ | ||
if (is_null($queryParam)) { | ||
return Modify::value($url)->addQueryParam()->fetch(); | ||
} | ||
|
||
return Modify::value($url)->addQueryParam($queryParam)->fetch(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Tests\Modifiers; | ||
|
||
use Statamic\Modifiers\Modify; | ||
use Tests\TestCase; | ||
|
||
class RemoveQueryParamTest extends TestCase | ||
{ | ||
protected $baseUrl = 'https://www.google.com/search'; | ||
protected $queryParamKey = 'q'; | ||
|
||
/** @test */ | ||
public function it_removes_an_existing_query_param() | ||
{ | ||
$this->assertSame($this->baseUrl, $this->modify("{$this->baseUrl}?q=statamic", $this->queryParamKey)); | ||
$this->assertSame("{$this->baseUrl}#test", $this->modify("{$this->baseUrl}?q=statamic#test", $this->queryParamKey)); | ||
$this->assertSame("{$this->baseUrl}?sourceid=chrome", $this->modify("{$this->baseUrl}?q=statamic&sourceid=chrome", $this->queryParamKey)); | ||
$this->assertSame("{$this->baseUrl}?sourceid=chrome", $this->modify("{$this->baseUrl}?sourceid=chrome&q=statamic", $this->queryParamKey)); | ||
} | ||
|
||
/** @test */ | ||
public function it_does_nothing_if_the_query_param_key_does_not_exist() | ||
{ | ||
$this->assertSame($this->baseUrl, $this->modify($this->baseUrl, $this->queryParamKey)); | ||
$this->assertSame("{$this->baseUrl}#test", $this->modify("{$this->baseUrl}#test", $this->queryParamKey)); | ||
$this->assertSame("{$this->baseUrl}?sourceid=chrome", $this->modify("{$this->baseUrl}?sourceid=chrome", $this->queryParamKey)); | ||
} | ||
|
||
/** @test */ | ||
public function it_does_nothing_if_no_parameters_are_passed() | ||
{ | ||
$this->assertSame($this->baseUrl, $this->modify($this->baseUrl)); | ||
} | ||
|
||
private function modify(string $url, ?string $queryParamKey = null) | ||
{ | ||
if (is_null($queryParamKey)) { | ||
return Modify::value($url)->removeQueryParam()->fetch(); | ||
} | ||
|
||
return Modify::value($url)->removeQueryParam($queryParamKey)->fetch(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Tests\Modifiers; | ||
|
||
use Statamic\Modifiers\Modify; | ||
use Tests\TestCase; | ||
|
||
class SetQueryParamTest extends TestCase | ||
{ | ||
protected $baseUrl = 'https://www.google.com/search'; | ||
protected $queryParam = ['q', 'test']; | ||
|
||
/** @test */ | ||
public function it_updates_an_existing_query_param() | ||
{ | ||
$this->assertSame("{$this->baseUrl}?q=", $this->modify("{$this->baseUrl}?q=statamic", ['q'])); | ||
$this->assertSame("{$this->baseUrl}?q=test", $this->modify("{$this->baseUrl}?q=statamic", $this->queryParam)); | ||
$this->assertSame("{$this->baseUrl}?q=test#test", $this->modify("{$this->baseUrl}?q=statamic#test", $this->queryParam)); | ||
$this->assertSame("{$this->baseUrl}?q=test&sourceid=chrome", $this->modify("{$this->baseUrl}?q=statamic&sourceid=chrome", $this->queryParam)); | ||
$this->assertSame("{$this->baseUrl}?sourceid=chrome&q=test", $this->modify("{$this->baseUrl}?sourceid=chrome&q=statamic", $this->queryParam)); | ||
} | ||
|
||
/** @test */ | ||
public function it_adds_a_non_existant_query_param() | ||
{ | ||
$this->assertSame("{$this->baseUrl}?q=", $this->modify($this->baseUrl, ['q'])); | ||
$this->assertSame("{$this->baseUrl}?q=test", $this->modify($this->baseUrl, $this->queryParam)); | ||
$this->assertSame("{$this->baseUrl}?q=test#test", $this->modify("{$this->baseUrl}#test", $this->queryParam)); | ||
$this->assertSame("{$this->baseUrl}?sourceid=chrome&q=test", $this->modify("{$this->baseUrl}?sourceid=chrome", $this->queryParam)); | ||
} | ||
|
||
/** @test */ | ||
public function it_does_nothing_if_no_parameters_are_passed() | ||
{ | ||
$this->assertSame($this->baseUrl, $this->modify($this->baseUrl)); | ||
} | ||
|
||
private function modify(string $url, ?array $queryParam = null) | ||
{ | ||
if (is_null($queryParam)) { | ||
return Modify::value($url)->setQueryParam()->fetch(); | ||
} | ||
|
||
return Modify::value($url)->setQueryParam($queryParam)->fetch(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters