Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #35 (Dots in page URL break calendar navigation)
The "page URLs" of CMSimple_XH are very special; they are usually not processed by the usual PHP features, and as such are not subject to the PHP URL parameter name replacements (spaces and periods are replaced by underscores). Thus, we must not process them with `http_build_query()`, but rather refer to them via `$su`. To avoid doing this in `CalendarController::modifyUrl()`, thereby coupling the unrelated URL handling more to the controller, we extract a simple `Url` class which hopefully does the job properly. A nice side-effect of this new URL handling is that we automatically produce more CMSimple_XH-like URLs for the starting page, whose "page URL" is empty. This is why we had to update a couple of tests.
- Loading branch information
Showing
21 changed files
with
200 additions
and
65 deletions.
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
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,56 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2023 Christoph M. Becker | ||
* | ||
* This file is part of Ocal_XH. | ||
* | ||
* Ocal_XH is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Ocal_XH is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Ocal_XH. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace Ocal; | ||
|
||
class Url | ||
{ | ||
/** @var string */ | ||
private $base; | ||
|
||
/** @var string */ | ||
private $page; | ||
|
||
/** @var array<string,string> */ | ||
private $params; | ||
|
||
/** @param array<string,string> $params */ | ||
public function __construct(string $base, string $page, array $params) | ||
{ | ||
$this->base = $base; | ||
$this->page = $page; | ||
$this->params = $params; | ||
} | ||
|
||
/** @param array<string,string> $params */ | ||
public function replace(array $params): self | ||
{ | ||
return new Url($this->base, $this->page, array_replace($this->params, $params)); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
$rest = http_build_query($this->params, "", "&", PHP_QUERY_RFC3986); | ||
$rest = preg_replace('/=(?=&|$)/', '', $rest); | ||
$query = $this->page . ($rest !== "" ? "&{$rest}": ""); | ||
return $this->base . ($query !== "" ? "?{$query}" : ""); | ||
} | ||
} |
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
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,86 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2023 Christoph M. Becker | ||
* | ||
* This file is part of Ocal_XH. | ||
* | ||
* Ocal_XH is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Ocal_XH is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Ocal_XH. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace Ocal; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class UrlTest extends TestCase | ||
{ | ||
/** @dataProvider dataForUrlHasDesiredStringRepresentation */ | ||
public function testUrlHasDesiredStringRepresentation(Url $url, string $expected): void | ||
{ | ||
$actual = (string) $url; | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public function dataForUrlHasDesiredStringRepresentation(): array | ||
{ | ||
return [ | ||
'no_page' => [new Url("/", "", []), "/"], | ||
'with_page' => [new Url("/", "Page", []), "/?Page"], | ||
'with_params' => [new Url("/", "Page", ["foo" => "bar", "qux" => "bar"]), "/?Page&foo=bar&qux=bar"], | ||
'params_but_no_page' => [new Url("/", "", ["foo" => "bar"]), "/?&foo=bar"], | ||
'gh-35' => [new Url("/", "Ocal-1.0", []), "/?Ocal-1.0"], | ||
]; | ||
} | ||
|
||
/** | ||
* @param array<string,string> $params | ||
* @dataProvider dataForUrlCanBeModified | ||
*/ | ||
public function testUrlCanBeModified(Url $url, array $params, string $expected): void | ||
{ | ||
$actual = (string) $url->replace($params); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public function dataForUrlCanBeModified(): array | ||
{ | ||
return [ | ||
[ | ||
new Url("/", "Page", []), | ||
[], | ||
"/?Page", | ||
], | ||
[ | ||
new Url("/", "", []), | ||
["foo" => "bar"], | ||
"/?&foo=bar", | ||
], | ||
[ | ||
new Url("/", "Page", ["foo" => "bar"]), | ||
[], | ||
"/?Page&foo=bar", | ||
], | ||
[ | ||
new Url("/", "Page", ["foo" => "bar"]), | ||
["baz" => "qux"], | ||
"/?Page&foo=bar&baz=qux", | ||
], | ||
[ | ||
new Url("/", "Page", ["foo" => "bar"]), | ||
["foo" => "baz"], | ||
"/?Page&foo=baz", | ||
], | ||
]; | ||
} | ||
} |
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
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
Oops, something went wrong.