PathHasAliasTest.php in Rules 8.3
File
tests/src/Unit/Integration/Condition/PathHasAliasTest.php
View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\Condition;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase;
use Drupal\path_alias\AliasManagerInterface;
class PathHasAliasTest extends RulesIntegrationTestBase {
protected $condition;
protected $aliasManager;
protected $englishLanguage;
protected function setUp() : void {
parent::setUp();
$this
->enableModule('path_alias');
$this->aliasManager = $this
->prophesize(AliasManagerInterface::class);
$this->container
->set('path_alias.manager', $this->aliasManager
->reveal());
$this->condition = $this->conditionManager
->createInstance('rules_path_has_alias');
$this->englishLanguage = $this
->prophesize(LanguageInterface::class);
$this->englishLanguage
->getId()
->willReturn('en');
}
public function testConstructor() {
$property = new \ReflectionProperty($this->condition, 'aliasManager');
$property
->setAccessible(TRUE);
$this
->assertSame($this->aliasManager
->reveal(), $property
->getValue($this->condition));
}
public function testConditionEvaluationPathWithAlias() {
$this->aliasManager
->getAliasByPath('/path-with-alias', NULL)
->willReturn('/alias-for-path')
->shouldBeCalledTimes(1);
$this->aliasManager
->getAliasByPath('/path-with-alias', 'en')
->willReturn('/alias-for-path')
->shouldBeCalledTimes(1);
$this->condition
->setContextValue('path', '/path-with-alias');
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('language', $this->englishLanguage
->reveal());
$this
->assertTrue($this->condition
->evaluate());
}
public function testConditionEvaluationPathWithoutAlias() {
$this->aliasManager
->getAliasByPath('/path-without-alias', NULL)
->willReturn('/path-without-alias')
->shouldBeCalledTimes(1);
$this->aliasManager
->getAliasByPath('/path-without-alias', 'en')
->willReturn('/path-without-alias')
->shouldBeCalledTimes(1);
$this->condition
->setContextValue('path', '/path-without-alias');
$this
->assertFalse($this->condition
->evaluate());
$this->condition
->setContextValue('language', $this->englishLanguage
->reveal());
$this
->assertFalse($this->condition
->evaluate());
}
}