EntityPathAliasCreateTest.php in Rules 8.3
File
tests/src/Unit/Integration/RulesAction/EntityPathAliasCreateTest.php
View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\RulesAction;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Url;
use Drupal\Tests\rules\Unit\Integration\RulesEntityIntegrationTestBase;
use Drupal\path_alias\PathAliasInterface;
use Prophecy\Argument;
class EntityPathAliasCreateTest extends RulesEntityIntegrationTestBase {
protected $action;
protected $aliasStorage;
protected function setUp() : void {
parent::setUp();
$this
->enableModule('path_alias');
$this->aliasStorage = $this
->prophesize(EntityStorageInterface::class);
$this->entityTypeManager
->getStorage('path_alias')
->willReturn($this->aliasStorage
->reveal());
$this->action = $this->actionManager
->createInstance('rules_entity_path_alias_create:entity:test');
}
public function testSummary() {
$this
->assertEquals('Create a test path alias', $this->action
->summary());
}
public function testActionExecutionWithUnsavedEntity() {
$path_alias = $this
->prophesizeEntity(PathAliasInterface::class);
$path_alias
->save()
->shouldBeCalledTimes(1);
$this->aliasStorage
->create([
'path' => '/test/1',
'alias' => '/about',
'langcode' => 'en',
])
->willReturn($path_alias
->reveal())
->shouldBeCalledTimes(1);
$entity = $this
->getMockEntity();
$entity
->isNew()
->willReturn(TRUE)
->shouldBeCalledTimes(1);
$entity
->save()
->shouldBeCalledTimes(1);
$this->action
->setContextValue('entity', $entity
->reveal())
->setContextValue('alias', '/about');
$this->action
->execute();
}
public function testActionExecutionWithSavedEntity() {
$path_alias = $this
->prophesizeEntity(PathAliasInterface::class);
$path_alias
->save()
->shouldBeCalledTimes(1);
$this->aliasStorage
->create([
'path' => '/test/1',
'alias' => '/about',
'langcode' => 'en',
])
->willReturn($path_alias
->reveal())
->shouldBeCalledTimes(1);
$entity = $this
->getMockEntity();
$entity
->isNew()
->willReturn(FALSE)
->shouldBeCalledTimes(1);
$entity
->save()
->shouldNotBeCalled();
$this->action
->setContextValue('entity', $entity
->reveal())
->setContextValue('alias', '/about');
$this->action
->execute();
}
protected function getMockEntity() {
$language = $this->languageManager
->reveal()
->getCurrentLanguage();
$entity = $this
->prophesizeEntity(EntityInterface::class);
$entity
->language()
->willReturn($language)
->shouldBeCalledTimes(1);
$url = $this
->prophesize(Url::class);
$url
->getInternalPath()
->willReturn('test/1')
->shouldBeCalledTimes(1);
$entity
->toUrl(Argument::any())
->willReturn($url
->reveal())
->shouldBeCalledTimes(1);
return $entity;
}
}