IsPathAliasForUnpublishedContentTest.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_publisher/tests/src/Unit/EventSubscriber/EntityEligibility/IsPathAliasForUnpublishedContentTest.php
View source
<?php
namespace Drupal\Tests\acquia_contenthub_publisher\Unit\EventSubscriber\EntityEligibility;
use Drupal\acquia_contenthub_publisher\EntityModeratedRevision;
use Drupal\acquia_contenthub_publisher\Event\ContentHubEntityEligibilityEvent;
use Drupal\acquia_contenthub_publisher\EventSubscriber\EnqueueEligibility\IsPathAliasForUnpublishedContent;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\node\NodeInterface;
use Drupal\path_alias\Entity\PathAlias;
use Drupal\Tests\acquia_contenthub\Unit\Helpers\LoggerMock;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
class IsPathAliasForUnpublishedContentTest extends UnitTestCase {
protected $matcher;
protected $entityModeratedRevision;
public function setup() {
parent::setUp();
$this->matcher = $this
->prophesize(UrlMatcherInterface::class);
$this->entityModeratedRevision = $this
->prophesize(EntityModeratedRevision::class);
}
public function testPathAliasForUnpublishedContent() {
$pnode = $this
->prophesize(NodeInterface::class);
$pnode
->id()
->willReturn(1);
$node = $pnode
->reveal();
$this->entityModeratedRevision
->isPublishedRevision($node)
->willReturn(TRUE, FALSE);
$ppathalias = $this
->prophesize(PathAlias::class);
$ppathalias
->getPath()
->willReturn('node/' . $node
->id());
$pathalias = $ppathalias
->reveal();
$params = [
'_raw_variables' => new class {
public function keys() : array {
return [
'node',
];
}
},
'node' => $node,
];
$this->matcher
->match($pathalias
->getPath())
->willReturn($params);
$subscriber = new IsPathAliasForUnpublishedContent($this->matcher
->reveal(), $this->entityModeratedRevision
->reveal(), new LoggerMock());
$event = new ContentHubEntityEligibilityEvent($pathalias, 'insert');
$subscriber
->onEnqueueCandidateEntity($event);
$this
->assertTrue($event
->getEligibility());
$this
->assertFalse($event
->isPropagationStopped());
$event = new ContentHubEntityEligibilityEvent($pathalias, 'update');
$subscriber
->onEnqueueCandidateEntity($event);
$this
->assertFalse($event
->getEligibility());
$this
->assertTrue($event
->isPropagationStopped());
}
public function testExceptionForPathAliases() {
$logger = new LoggerMock();
$ppathalias = $this
->prophesize(PathAlias::class);
$ppathalias
->uuid()
->willReturn('random-uuid');
$ppathalias
->getPath()
->willReturn('node/1');
$pathalias = $ppathalias
->reveal();
$exception_message = 'Matcher is not valid.';
$this->matcher
->match($pathalias
->getPath())
->willThrow(new \Exception($exception_message));
$subscriber = new IsPathAliasForUnpublishedContent($this->matcher
->reveal(), $this->entityModeratedRevision
->reveal(), $logger);
$event = new ContentHubEntityEligibilityEvent($pathalias, 'insert');
$this
->assertEmpty($logger
->getLogMessages());
$subscriber
->onEnqueueCandidateEntity($event);
$log_messages = $logger
->getLogMessages();
$this
->assertNotEmpty($log_messages);
$this
->assertNotEmpty($log_messages[RfcLogLevel::ERROR]);
$this
->assertEquals('Following error occurred while trying to get the matching entity for path alias with uuid: random-uuid. Error: ' . $exception_message, $log_messages[RfcLogLevel::ERROR][0]);
$this
->assertFalse($event
->getEligibility());
$this
->assertTrue($event
->isPropagationStopped());
}
}