Skip to content

Commit 091e69c

Browse files
committed
MNT Unit test for versioning actions
1 parent 59d087d commit 091e69c

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/Controllers/RestApiEndpoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private function apiView(): HTTPResponse
306306
}
307307
return $this->apiViewMany($request);
308308
}
309-
309+
310310
/**
311311
* View one value from the API - used for GET requests
312312
*/

tests/Controllers/RestApiEndpointTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestCanMethodStatic;
1818
use emteknetnz\RestApi\Exceptions\RestApiEndpointConfigException;
1919
use SilverStripe\Security\SecurityToken;
20-
use SilverStripe\Versioned\Versioned;
20+
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestVersionedExtension;
21+
2122

2223
# vendor/bin/phpunit app/tests/Controllers/RestApiTest.php flush=1
2324

@@ -83,6 +84,7 @@ protected function setUp(): void
8384
SecurityToken::enable();
8485
TestCanMethodStatic::setCanMethodsThatPass(self::VIEW_CREATE_EDIT_DELETE_ACTION);
8586
TestApiEndpoint::resetHooksCalled();
87+
TestVersionedExtension::enableAutoPublish();
8688
$this->setConfig(self::PATH, $this->endpointPath);
8789
// Create fixtures
8890
$testTeam = TestTeam::create([
@@ -2302,6 +2304,30 @@ public function testExtensionHooks(): void
23022304
$this->assertTrue(TestApiEndpoint::$hooksCalled['updateApiConfig']);
23032305
}
23042306

2307+
public function testVersionedActions(): void
2308+
{
2309+
// Set ACCESS to LOGGED_IN to set versioned mode to DRAFT in rest-api
2310+
$this->setConfig(self::ACCESS, self::LOGGED_IN);
2311+
$this->login(self::AUTH_LEVEL_LOGGED_IN);
2312+
TestVersionedExtension::disableAutoPublish();
2313+
$task = TestTask::get()->first();
2314+
$taskID = $task->ID;
2315+
// create a draft change
2316+
$this->req('PATCH', $taskID, null, null, ['title' => 'Updated'])->getStatusCode();
2317+
// assert versioning actions
2318+
$task = TestTask::get()->byID($taskID);
2319+
$this->assertTrue($task->stagesDiffer());
2320+
$this->req('PUT', $taskID, 'publish');
2321+
$task = TestTask::get()->byID($taskID);
2322+
$this->assertFalse($task->stagesDiffer());
2323+
$this->req('PUT', $taskID, 'unpublish');
2324+
$task = TestTask::get()->byID($taskID);
2325+
$this->assertTrue($task->stagesDiffer());
2326+
$this->req('PUT', $taskID, 'archive');
2327+
$task = TestTask::get()->byID($taskID);
2328+
$this->assertNull($task);
2329+
}
2330+
23052331
private function login(int $authLevel)
23062332
{
23072333
if ($authLevel === self::AUTH_LEVEL_NONE) {

tests/Controllers/RestApiEndpointTest/TestVersionedExtension.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,25 @@
1111
*/
1212
class TestVersionedExtension extends DataExtension implements TestOnly
1313
{
14+
/**
15+
* @internal
16+
*/
17+
private static $autoPublish = false;
18+
19+
public static function enableAutoPublish(): void
20+
{
21+
self::$autoPublish = true;
22+
}
23+
24+
public static function disableAutoPublish(): void
25+
{
26+
self::$autoPublish = false;
27+
}
28+
1429
public function onAfterWrite()
1530
{
16-
$this->owner->publishRecursive();
31+
if (self::$autoPublish) {
32+
$this->owner->publishRecursive();
33+
}
1734
}
1835
}

0 commit comments

Comments
 (0)