From 9823eda22e0602dfb8d93f5282e006f70c6dc072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Horsk=C3=BD?= Date: Fri, 3 Jan 2020 22:37:19 +0100 Subject: [PATCH] Presenter: added isModuleCurrent() [Closes #240] (#243) --- src/Application/UI/Presenter.php | 6 +++ tests/UI/Presenter.isModuleCurrent().phpt | 53 +++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/UI/Presenter.isModuleCurrent().phpt diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index dcfd55058..44909187c 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -188,6 +188,12 @@ final public function getModule(): string } + public function isModuleCurrent(string $moduleName): bool + { + return Nette\Utils\Strings::startsWith($this->getModule() . ':', ltrim($moduleName . ':', ':')); + } + + /********************* interface IPresenter ****************d*g**/ diff --git a/tests/UI/Presenter.isModuleCurrent().phpt b/tests/UI/Presenter.isModuleCurrent().phpt new file mode 100644 index 000000000..dcdc9df23 --- /dev/null +++ b/tests/UI/Presenter.isModuleCurrent().phpt @@ -0,0 +1,53 @@ +setParent(null, 'Test'); + + Assert::same('', $presenter->getModule()); + + Assert::true($presenter->isModuleCurrent('')); + Assert::false($presenter->isModuleCurrent('Test')); + Assert::false($presenter->isModuleCurrent(':Test')); +}); + + +test(function () { + $presenter = new TestPresenter; + $presenter->setParent(null, 'First:Second:Third:Test'); + + Assert::same('First:Second:Third', $presenter->getModule()); + + Assert::false($presenter->isModuleCurrent('First:Second:Third:Test')); + + Assert::true($presenter->isModuleCurrent('First:Second:Third')); + Assert::true($presenter->isModuleCurrent('First:Second')); + Assert::true($presenter->isModuleCurrent('First')); + Assert::true($presenter->isModuleCurrent('')); + + Assert::true($presenter->isModuleCurrent(':First:Second:Third')); + Assert::true($presenter->isModuleCurrent(':First:Second')); + Assert::true($presenter->isModuleCurrent(':First')); + Assert::true($presenter->isModuleCurrent(':')); + + Assert::false($presenter->isModuleCurrent('First:Second:Other')); + Assert::false($presenter->isModuleCurrent('First:Other')); + Assert::false($presenter->isModuleCurrent('First:Second:T')); + Assert::false($presenter->isModuleCurrent('First:S')); +});