Skip to content

Commit

Permalink
Presenter: added isModuleCurrent() [Closes #240] (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
khorsky authored and dg committed Jan 5, 2020
1 parent 89a989b commit 558bcba
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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**/


Expand Down
53 changes: 53 additions & 0 deletions tests/UI/Presenter.isModuleCurrent().phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* Test: Presenter::isModuleCurrent.
*/

declare(strict_types = 1);

use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


class TestPresenter extends \Nette\Application\UI\Presenter
{
}


test(function () {
$presenter = new TestPresenter;
$presenter->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'));
});

0 comments on commit 558bcba

Please sign in to comment.