PathAliasExistsTest.php in Rules 8.3
File
tests/src/Unit/Integration/Condition/PathAliasExistsTest.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 PathAliasExistsTest 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_alias_exists');
$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 testConditionEvaluationAliasWithPath() {
$this->aliasManager
->getPathByAlias('/alias-for-path', NULL)
->willReturn('/path-with-alias')
->shouldBeCalledTimes(1);
$this->aliasManager
->getPathByAlias('/alias-for-path', 'en')
->willReturn('/path-with-alias')
->shouldBeCalledTimes(1);
$this->condition
->setContextValue('alias', '/alias-for-path');
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('language', $this->englishLanguage
->reveal());
$this
->assertTrue($this->condition
->evaluate());
}
public function testConditionEvaluationAliasWithoutPath() {
$this->aliasManager
->getPathByAlias('/alias-for-path-that-does-not-exist', NULL)
->willReturn('/alias-for-path-that-does-not-exist')
->shouldBeCalledTimes(1);
$this->aliasManager
->getPathByAlias('/alias-for-path-that-does-not-exist', 'en')
->willReturn('/alias-for-path-that-does-not-exist')
->shouldBeCalledTimes(1);
$this->condition
->setContextValue('alias', '/alias-for-path-that-does-not-exist');
$this
->assertFalse($this->condition
->evaluate());
$this->condition
->setContextValue('language', $this->englishLanguage
->reveal());
$this
->assertFalse($this->condition
->evaluate());
}
}