Skip to content

Commit 808d883

Browse files
committed
Fix versioning
1 parent dd29c0f commit 808d883

File tree

10 files changed

+76
-15
lines changed

10 files changed

+76
-15
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ Example:
240240

241241
### DELETE<a name="readme-http-delete"></a>
242242

243-
The `DELETE` HTTP request is used to delete an existing record where ID is specified in the URL.
243+
The `DELETE` HTTP request is used to delete an existing record where ID is specified in the URL. If the `Versioned` extension from the `silverstripe/versioned` module has been applied to the DataObject then the `doArchive()` method is called on the DataObject which deletes it from other the draft and live versions of the site. If the `Versioned` extension has not bee applied then the `delete()` method will be called on DataObject instead.
244244

245245
Example:
246246
- `curl -X DELETE https://mysite.test/api/pages/123`
@@ -258,9 +258,9 @@ Actions can only be run on existing records. The action parameter is added to th
258258
The following actions are available:
259259
| Action | Description |
260260
| - | - |
261-
| `publish` | Publish a record. Requires `silverstripe/versioned` to be installed. |
262-
| `unpublish` | Unpublish a record. Requires `silverstripe/versioned` to be installed. |
263-
| `archive` | Archive a record. Requires `silverstripe/versioned` to be installed. |
261+
| `publish` | Publish a record. Requires the `Versioned` extension from `silverstripe/versioned` module to be applied to the `DataObject`. |
262+
| `unpublish` | Unpublish a record. Requires the `Versioned` extension from `silverstripe/versioned` module to be applied to the `DataObject`. |
263+
| `archive` | Archive a record. Requires the `Versioned` extension from `silverstripe/versioned` module to be applied to the `DataObject`. Note this similar to using the `DELETE` HTTP method. |
264264

265265
Example:
266266
- `curl -X PUT https://mysite.test/api/pages/123/publish`

src/Controllers/RestApiEndpoint.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,11 @@ private function apiDelete(): HTTPResponse
564564
}
565565
$obj = $this->dataObjectFromRequest($dataClass);
566566
$this->invokeWithExtensions('onDeleteBeforeDelete', $obj);
567-
$obj->delete();
567+
if ($obj->hasExtension(Versioned::class)) {
568+
$obj->doArchive();
569+
} else {
570+
$obj->delete();
571+
}
568572
$this->invokeWithExtensions('onDeleteAfterDelete', $obj);
569573
// Will default to a 200 status code
570574
// A 204 status code is not used because there is stil JSON content returned in the body
@@ -597,11 +601,10 @@ private function apiAction(): HTTPResponse
597601
*/
598602
private function apiVersioning(DataObject $obj, string $action): HTTPResponse
599603
{
600-
$dataClass = $this->endpointConfig(self::DATA_CLASS, false);
601604
$callCanMethods = $this->endpointConfig(self::CALL_CAN_METHODS, true);
602605
$doCheck = $this->configContains($callCanMethods, self::ACTION);
603-
if (!$dataClass::singleton()->hasExtension(Versioned::class)) {
604-
$message = !Director::isDev() ? '' : "$dataClass does not support Versioning";
606+
if (!$obj->hasExtension(Versioned::class)) {
607+
$message = !Director::isDev() ? '' : get_class($obj) . ' does not support Versioning';
605608
throw new RestApiEndpointException($message, 404);
606609
}
607610
if ($action === 'publish') {

tests/Controllers/RestApiEndpointTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestCanMethodStatic;
1818
use emteknetnz\RestApi\Exceptions\RestApiEndpointConfigException;
1919
use SilverStripe\Security\SecurityToken;
20+
use SilverStripe\Versioned\Versioned;
2021

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

@@ -186,6 +187,10 @@ protected function setUp(): void
186187
'AllowedOperationsCreate' => 'TestTask 04 AllowedOperationsCreate',
187188
'AllowedOperationsEdit' => 'TestTask 04 AllowedOperationsEdit',
188189
])->write();
190+
// TestTask has the Versioned extension on it to support versioning tests
191+
foreach (TestTask::get() as $testTask) {
192+
$testTask->publishRecursive();
193+
}
189194
}
190195

191196
/**
@@ -2268,8 +2273,10 @@ public function testETag()
22682273

22692274
public function testExtensionHooks(): void
22702275
{
2276+
$task = TestTask::get()->first();
2277+
$taskID = $task->ID;
22712278
// ViewOne
2272-
$this->req('GET', TestTask::get()->first()->ID);
2279+
$this->req('GET', $taskID);
22732280
$this->assertTrue(TestApiEndpoint::$hooksCalled['onViewOne']);
22742281
// ViewMany
22752282
$this->req('GET');
@@ -2279,17 +2286,17 @@ public function testExtensionHooks(): void
22792286
$this->assertTrue(TestApiEndpoint::$hooksCalled['onCreateBeforeWrite']);
22802287
$this->assertTrue(TestApiEndpoint::$hooksCalled['onCreateAfterWrite']);
22812288
// Edit
2282-
$this->req('PATCH', TestTask::get()->first()->ID, null, null, ['title' => 'test']);
2289+
$this->req('PATCH', $taskID, null, null, ['title' => 'test']);
22832290
$this->assertTrue(TestApiEndpoint::$hooksCalled['onEditBeforeWrite']);
22842291
$this->assertTrue(TestApiEndpoint::$hooksCalled['onEditAfterWrite']);
2285-
// Delete
2286-
$this->req('DELETE', TestTask::get()->first()->ID);
2287-
$this->assertTrue(TestApiEndpoint::$hooksCalled['onDeleteBeforeDelete']);
2288-
$this->assertTrue(TestApiEndpoint::$hooksCalled['onDeleteAfterDelete']);
22892292
// Action
2290-
$this->req('ACTION', TestTask::get()->first()->ID, 'publish');
2293+
$this->req('PUT', $taskID, 'publish');
22912294
$this->assertTrue(TestApiEndpoint::$hooksCalled['onBeforeAction']);
22922295
$this->assertTrue(TestApiEndpoint::$hooksCalled['onAfterAction']);
2296+
// Delete - need to call this after all the others
2297+
$this->req('DELETE', $taskID);
2298+
$this->assertTrue(TestApiEndpoint::$hooksCalled['onDeleteBeforeDelete']);
2299+
$this->assertTrue(TestApiEndpoint::$hooksCalled['onDeleteAfterDelete']);
22932300
// BeforeSendResponse
22942301
$this->assertTrue(TestApiEndpoint::$hooksCalled['onBeforeSendResponse']);
22952302
}

tests/Controllers/RestApiEndpointTest/TestApiEndpoint.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class TestApiEndpoint extends RestApiEndpoint implements PermissionProvider, Tes
118118
'onEditAfterWrite' => false,
119119
'onDeleteBeforeDelete' => false,
120120
'onDeleteAfterDelete' => false,
121+
'onBeforeAction' => false,
122+
'onAfterAction' => false,
121123
'onBeforeSendResponse' => false,
122124
];
123125

tests/Controllers/RestApiEndpointTest/TestMilestone.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestProject;
77
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestCanMethodsTrait;
88
use SilverStripe\ORM\DataObject;
9+
use SilverStripe\Versioned\Versioned;
910

1011
class TestMilestone extends DataObject implements TestOnly
1112
{
@@ -20,4 +21,9 @@ class TestMilestone extends DataObject implements TestOnly
2021
];
2122

2223
private static $table_name = 'TestMilestone';
24+
25+
private static $extensions = [
26+
Versioned::class,
27+
TestVersionedExtension::class,
28+
];
2329
}

tests/Controllers/RestApiEndpointTest/TestProject.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestMilestone;
99
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestCanMethodsTrait;
1010
use SilverStripe\ORM\DataObject;
11+
use SilverStripe\Versioned\Versioned;
1112

1213
class TestProject extends DataObject implements TestOnly
1314
{
@@ -27,4 +28,9 @@ class TestProject extends DataObject implements TestOnly
2728
];
2829

2930
private static $table_name = 'TestProject';
31+
32+
private static $extensions = [
33+
Versioned::class,
34+
TestVersionedExtension::class,
35+
];
3036
}

tests/Controllers/RestApiEndpointTest/TestReporter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use SilverStripe\Dev\TestOnly;
66
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestCanMethodsTrait;
77
use SilverStripe\ORM\DataObject;
8+
use SilverStripe\Versioned\Versioned;
89

910
class TestReporter extends DataObject implements TestOnly
1011
{
@@ -15,4 +16,9 @@ class TestReporter extends DataObject implements TestOnly
1516
];
1617

1718
private static $table_name = 'TestReporter';
19+
20+
private static $extensions = [
21+
Versioned::class,
22+
TestVersionedExtension::class,
23+
];
1824
}

tests/Controllers/RestApiEndpointTest/TestTask.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestCanMethodsTrait;
99
use SilverStripe\ORM\DataObject;
1010
use SilverStripe\ORM\ValidationResult;
11+
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestVersionedExtension;
12+
use SilverStripe\Versioned\Versioned;
1113

1214
class TestTask extends DataObject implements TestOnly
1315
{
@@ -33,6 +35,11 @@ class TestTask extends DataObject implements TestOnly
3335
'TestReporter' => TestReporter::class,
3436
];
3537

38+
private static $extensions = [
39+
Versioned::class,
40+
TestVersionedExtension::class,
41+
];
42+
3643
private static $table_name = 'TestTask';
3744

3845
public function validate()

tests/Controllers/RestApiEndpointTest/TestTeam.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestProject;
77
use emteknetnz\RestApi\Tests\Controllers\RestApiTest\TestCanMethodsTrait;
88
use SilverStripe\ORM\DataObject;
9+
use SilverStripe\Versioned\Versioned;
910

1011
class TestTeam extends DataObject implements TestOnly
1112
{
@@ -48,4 +49,9 @@ class TestTeam extends DataObject implements TestOnly
4849
];
4950

5051
private static $table_name = 'TestTeam';
52+
53+
private static $extensions = [
54+
Versioned::class,
55+
TestVersionedExtension::class,
56+
];
5157
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace emteknetnz\RestApi\Tests\Controllers\RestApiTest;
4+
5+
use SilverStripe\Dev\TestOnly;
6+
use SilverStripe\ORM\DataExtension;
7+
use SilverStripe\Versioned\Versioned;
8+
9+
/**
10+
* Automatically publish a DataObject when it is written - this is to make asserting things easier
11+
*/
12+
class TestVersionedExtension extends DataExtension implements TestOnly
13+
{
14+
public function onAfterWrite()
15+
{
16+
$this->owner->publishRecursive();
17+
}
18+
}

0 commit comments

Comments
 (0)