Skip to content

Commit

Permalink
ENH Allow extensions to provide triggers (#214)
Browse files Browse the repository at this point in the history
Feature/trigger in dataextensions
  • Loading branch information
tiller1010 authored Jan 15, 2025
1 parent 8fe9f6c commit 5c0fda7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Extension/Engine/SiteTreePublishingEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function collectChanges($context)
}

// The page does not include the required extension, and it doesn't implement a Trigger
if (!$siteTree->hasExtension(PublishableSiteTree::class) && !($siteTree instanceof StaticPublishingTrigger)) {
if (!$siteTree->hasExtension(StaticPublishingTrigger::class) && !($siteTree instanceof StaticPublishingTrigger)) {
return;
}

Expand Down
35 changes: 35 additions & 0 deletions tests/php/Extension/Engine/SiteTreePublishingEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use SilverStripe\StaticPublishQueue\Job\GenerateStaticCacheJob;
use SilverStripe\StaticPublishQueue\Service\UrlBundleService;
use SilverStripe\StaticPublishQueue\Test\QueuedJobsTestService;
use SilverStripe\StaticPublishQueue\Test\StaticPublisherTest\Model\ExtensionAddsTrigger;
use SilverStripe\StaticPublishQueue\Test\StaticPublisherTest\Model\DataObjectNoTrigger;
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
use Symbiote\QueuedJobs\Services\QueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJobHandler;
Expand Down Expand Up @@ -232,6 +234,39 @@ public function testDoUnpublish(): void
$this->assertEqualsCanonicalizing($expectedUrls, $resultUrls);
}

public function testStaticPublishingTriggerOnExtension(): void
{
// Inclusion of parent/child is tested in PublishableSiteTreeTest
SiteTree::config()->set('regenerate_parents', PublishableSiteTree::REGENERATE_RELATIONS_NONE);
SiteTree::config()->set('regenerate_children', PublishableSiteTree::REGENERATE_RELATIONS_NONE);

DataObjectNoTrigger::add_extension(ExtensionAddsTrigger::class);
DataObjectNoTrigger::add_extension(SiteTreePublishingEngine::class);

/** @var QueuedJobsTestService $service */
$service = QueuedJobService::singleton();

$dataObject = DataObjectNoTrigger::create()->write();
$dataObject->publishRecursive();

$jobs = $service->getJobs();

// We should only have 1 job queued
$this->assertCount(1, $jobs);

// Let's grab the job and inspect the contents
/** @var GenerateStaticCacheJob $updateJob */
$updateJob = $this->getJobByClassName($jobs, GenerateStaticCacheJob::class);

$expectedUrls = [
'http://example.com/subpage/dataobject-1',
];
$resultUrls = array_keys($updateJob->getJobData()->jobData->URLsToProcess);

$this->assertInstanceOf(GenerateStaticCacheJob::class, $updateJob);
$this->assertEqualsCanonicalizing($expectedUrls, $resultUrls);
}

protected function getJobByClassName(array $jobs, string $className): ?QueuedJob
{
foreach ($jobs as $job) {
Expand Down
15 changes: 15 additions & 0 deletions tests/php/StaticPublisherTest/Model/DataObjectNoTrigger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace SilverStripe\StaticPublishQueue\Test\StaticPublisherTest\Model;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;

class DataObjectNoTrigger extends DataObject implements TestOnly
{
public function AbsoluteLink()
{
return 'http://example.com/subpage/dataobject-1';
}
}

27 changes: 27 additions & 0 deletions tests/php/StaticPublisherTest/Model/ExtensionAddsTrigger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace SilverStripe\StaticPublishQueue\Test\StaticPublisherTest\Model;

use SilverStripe\Dev\TestOnly;
use SilverStripe\Core\Extension;
use SilverStripe\StaticPublishQueue\Contract\StaticPublishingTrigger;
use SilverStripe\StaticPublishQueue\Contract\StaticallyPublishable;

class ExtensionAddsTrigger extends Extension implements StaticallyPublishable, StaticPublishingTrigger, TestOnly
{
public function urlsToCache()
{
return [$this->owner->AbsoluteLink() => 0];
}

public function objectsToUpdate($context)
{
return $this->owner;
}

public function objectsToDelete($context)
{
return [];
}
}

0 comments on commit 5c0fda7

Please sign in to comment.