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 3, 2020
1 parent 683b1fb commit 23d1851
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ final public function getModule(): string
}


public function isModuleCurrent(string $moduleName): bool
{
if (!$moduleName) {
throw new Nette\InvalidArgumentException('Missing module name!');
}

$moduleName = trim($moduleName, ':') . ':';

return Nette\Utils\Strings::startsWith($this->getModule() . ':', $moduleName);
}


/********************* interface IPresenter ****************d*g**/


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

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

declare(strict_types = 1);

use Tester\Assert;

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


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


Assert::exception(
function (): void {
$presenter = new TestPresenter;
$presenter->isModuleCurrent('');
},
\Nette\InvalidArgumentException::class
);


test(function () {
$presenter = new TestPresenter;
$presenter->setParent(null, 'Test');

Assert::same('', $presenter->getModule());

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(':First:Second:Third'));
Assert::true($presenter->isModuleCurrent(':First:Second'));
Assert::true($presenter->isModuleCurrent(':First'));

Assert::true($presenter->isModuleCurrent('First:Second:Third:'));
Assert::true($presenter->isModuleCurrent('First:Second:'));
Assert::true($presenter->isModuleCurrent('First:'));

Assert::true($presenter->isModuleCurrent(':First:Second:Third:'));
Assert::true($presenter->isModuleCurrent(':First:Second:'));
Assert::true($presenter->isModuleCurrent(':First:'));

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 23d1851

Please sign in to comment.