From cde029be7fb495e3952d838f9d74e76da2d109ab Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Tue, 18 Jun 2024 19:15:58 +1200 Subject: [PATCH] ENH Remove unused legacy graphql code --- _config/graphql-legacy.yml | 33 -- composer.json | 5 +- .../DataObjectScaffolderExtensionTest.php | 70 ----- .../Legacy/Extensions/ReadExtensionTest.php | 59 ---- .../Extensions/ReadOneExtensionTest.php | 100 ------ .../SchemaScaffolderExtensionTest.php | 62 ---- .../Legacy/Operations/CopyToStageTest.php | 116 ------- .../GraphQL/Legacy/Operations/PublishTest.php | 86 ------ .../Legacy/Operations/ReadVersionsTest.php | 134 -------- .../Legacy/Operations/RollbackTest.php | 91 ------ .../Legacy/Operations/UnpublishTest.php | 89 ------ .../Resolvers/ApplyVersionFiltersTest.php | 291 ------------------ .../php/GraphQL/Plugins/VersionedReadTest.php | 2 +- 13 files changed, 2 insertions(+), 1136 deletions(-) delete mode 100644 _config/graphql-legacy.yml delete mode 100644 tests/php/GraphQL/Legacy/Extensions/DataObjectScaffolderExtensionTest.php delete mode 100644 tests/php/GraphQL/Legacy/Extensions/ReadExtensionTest.php delete mode 100644 tests/php/GraphQL/Legacy/Extensions/ReadOneExtensionTest.php delete mode 100644 tests/php/GraphQL/Legacy/Extensions/SchemaScaffolderExtensionTest.php delete mode 100644 tests/php/GraphQL/Legacy/Operations/CopyToStageTest.php delete mode 100644 tests/php/GraphQL/Legacy/Operations/PublishTest.php delete mode 100644 tests/php/GraphQL/Legacy/Operations/ReadVersionsTest.php delete mode 100644 tests/php/GraphQL/Legacy/Operations/RollbackTest.php delete mode 100644 tests/php/GraphQL/Legacy/Operations/UnpublishTest.php delete mode 100644 tests/php/GraphQL/Legacy/Resolvers/ApplyVersionFiltersTest.php diff --git a/_config/graphql-legacy.yml b/_config/graphql-legacy.yml deleted file mode 100644 index 48be804d..00000000 --- a/_config/graphql-legacy.yml +++ /dev/null @@ -1,33 +0,0 @@ ---- -Name: versioned-graphql -Only: - moduleexists: 'silverstripe/graphql' -Except: - classexists: 'SilverStripe\GraphQL\Schema\Schema' ---- -SilverStripe\GraphQL\Scaffolding\Scaffolders\CRUD\Read: - extensions: - - SilverStripe\Versioned\GraphQL\Extensions\ReadExtension -SilverStripe\GraphQL\Scaffolding\Scaffolders\CRUD\Delete: - extensions: - - SilverStripe\Versioned\GraphQL\Extensions\DeleteExtension -SilverStripe\GraphQL\Scaffolding\Scaffolders\CRUD\ReadOne: - extensions: - - SilverStripe\Versioned\GraphQL\Extensions\ReadExtension -SilverStripe\GraphQL\Scaffolding\Scaffolders\SchemaScaffolder: - extensions: - - SilverStripe\Versioned\GraphQL\Extensions\SchemaScaffolderExtension -SilverStripe\GraphQL\Scaffolding\Scaffolders\DataObjectScaffolder: - extensions: - - SilverStripe\Versioned\GraphQL\Extensions\DataObjectScaffolderExtension - -# Register the versioned operations -SilverStripe\GraphQL\Scaffolding\Scaffolders\OperationScaffolder: - operations: - copyToStage: SilverStripe\Versioned\GraphQL\Operations\CopyToStage - publish: SilverStripe\Versioned\GraphQL\Operations\Publish - unpublish: SilverStripe\Versioned\GraphQL\Operations\Unpublish - rollback: SilverStripe\Versioned\GraphQL\Operations\Rollback -SilverStripe\GraphQL\Manager: - extensions: - - SilverStripe\Versioned\GraphQL\Extensions\ManagerExtension diff --git a/composer.json b/composer.json index 884cc8ea..a0598ef2 100755 --- a/composer.json +++ b/composer.json @@ -33,10 +33,7 @@ }, "autoload": { "psr-4": { - "SilverStripe\\Versioned\\": [ - "src/", - "_legacy/" - ], + "SilverStripe\\Versioned\\": "src/", "SilverStripe\\Versioned\\Tests\\": "tests/php/" } }, diff --git a/tests/php/GraphQL/Legacy/Extensions/DataObjectScaffolderExtensionTest.php b/tests/php/GraphQL/Legacy/Extensions/DataObjectScaffolderExtensionTest.php deleted file mode 100644 index d06ddec2..00000000 --- a/tests/php/GraphQL/Legacy/Extensions/DataObjectScaffolderExtensionTest.php +++ /dev/null @@ -1,70 +0,0 @@ -markTestSkipped('Skipped GraphQL 3 test ' . __CLASS__); - } - } - - public function testDataObjectScaffolderAddsVersionedFields() - { - $manager = new Manager(); - $manager->addType((new VersionedStage())->toType()); - $manager->addType((new VersionSortType())->toType()); - $scaffolder = new DataObjectScaffolder(Fake::class); - $scaffolder->addFields(['Name', 'Title']); - $scaffolder->addToManager($manager); - $typeName = $scaffolder->getTypeName(); - - $type = $manager->getType($typeName); - $this->assertInstanceOf(ObjectType::class, $type); - $fields = $type->config['fields'](); - $this->assertArrayHasKey('Version', $fields); - $this->assertArrayHasKey('Versions', $fields); - $this->assertInstanceOf(IntType::class, $fields['Version']['type']); - $this->assertInstanceOf(ObjectType::class, $fields['Versions']['type']); - } - - public function testDataObjectScaffolderDoesntAddVersionedFieldsToUnversionedObjects() - { - Fake::remove_extension(Versioned::class); - $manager = new Manager(); - $manager->addType((new VersionedStage())->toType()); - $scaffolder = new DataObjectScaffolder(UnversionedWithField::class); - $scaffolder->addToManager($manager); - $typeName = $scaffolder->getTypeName(); - - $type = $manager->getType($typeName); - $this->assertInstanceOf(ObjectType::class, $type); - $fields = $type->config['fields'](); - $this->assertArrayNotHasKey('Version', $fields); - $this->assertArrayNotHasKey('Versions', $fields); - } -} diff --git a/tests/php/GraphQL/Legacy/Extensions/ReadExtensionTest.php b/tests/php/GraphQL/Legacy/Extensions/ReadExtensionTest.php deleted file mode 100644 index b3667a6a..00000000 --- a/tests/php/GraphQL/Legacy/Extensions/ReadExtensionTest.php +++ /dev/null @@ -1,59 +0,0 @@ -markTestSkipped('Skipped GraphQL 3 test ' . __CLASS__); - } - } - - public function testReadExtensionAppliesFilters() - { - $mock = $this->getMockBuilder(ApplyVersionFilters::class) - ->setMethods(['applyToList']) - ->getMock(); - $mock - ->expects($this->once()) - ->method('applyToList'); - - Injector::inst()->registerService($mock, ApplyVersionFilters::class); - - $manager = new Manager(); - $manager->addType((new VersionedInputType())->toType()); - $manager->addType(new ObjectType(['name' => StaticSchema::inst()->typeNameForDataObject(Fake::class)])); - $read = new Read(Fake::class); - $read->setUsePagination(false); - $readScaffold = $read->scaffold($manager); - $this->assertIsCallable($readScaffold['resolve']); - $readScaffold['resolve'](null, ['Versioning' => true], ['currentUser' => new Member()], new FakeResolveInfo()); - } -} diff --git a/tests/php/GraphQL/Legacy/Extensions/ReadOneExtensionTest.php b/tests/php/GraphQL/Legacy/Extensions/ReadOneExtensionTest.php deleted file mode 100644 index e385b95d..00000000 --- a/tests/php/GraphQL/Legacy/Extensions/ReadOneExtensionTest.php +++ /dev/null @@ -1,100 +0,0 @@ -markTestSkipped('Skipped GraphQL 3 test ' . __CLASS__); - } - } - - public function testReadOneExtensionAppliesFilters() - { - $manager = new Manager(); - $manager->addType((new VersionedInputType())->toType()); - $manager->addType(new ObjectType(['name' => StaticSchema::inst()->typeNameForDataObject(Fake::class)])); - $read = new ReadOne(Fake::class); - $readScaffold = $read->scaffold($manager); - $this->assertIsCallable($readScaffold['resolve']); - $doResolve = function ($mode, $ID, $version = null) use ($readScaffold) { - $args = [ - 'ID' => $ID, - 'Versioning' => [ - 'Mode' => $mode, - ], - ]; - if ($version) { - $args['Versioning']['Version'] = $version; - } - - return $readScaffold['resolve']( - null, - $args, - ['currentUser' => Security::getCurrentUser()], - new FakeResolveInfo() - ); - }; - - /* @var Fake|Versioned $record */ - $record = new Fake(); - $record->Name = 'First'; - $record->write(); - - $record->Name = 'Second'; - $record->write(); - - $result = $doResolve(Versioned::LIVE, $record->ID); - $this->assertNull($result); - - $result = $doResolve(Versioned::DRAFT, $record->ID); - $this->assertInstanceOf(Fake::class, $result); - $this->assertEquals($record->ID, $result->ID); - $this->assertEquals(2, $record->Version); - - $record->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $result = $doResolve(Versioned::LIVE, $record->ID); - $this->assertInstanceOf(Fake::class, $result); - $this->assertEquals($record->ID, $result->ID); - $this->assertEquals(2, $record->Version); - - $record->Name = 'Third'; - $record->write(); - - $result = $doResolve('version', $record->ID, 1); - $this->assertInstanceOf(Fake::class, $result); - $this->assertEquals($record->ID, $result->ID); - $this->assertEquals('First', $result->Name); - - $this->expectException(\InvalidArgumentException::class); - $doResolve('version', $record->ID, null); - } -} diff --git a/tests/php/GraphQL/Legacy/Extensions/SchemaScaffolderExtensionTest.php b/tests/php/GraphQL/Legacy/Extensions/SchemaScaffolderExtensionTest.php deleted file mode 100644 index 334f8f5b..00000000 --- a/tests/php/GraphQL/Legacy/Extensions/SchemaScaffolderExtensionTest.php +++ /dev/null @@ -1,62 +0,0 @@ -markTestSkipped('Skipped GraphQL 3 test ' . __CLASS__); - } - } - - public function testSchemaScaffolderEnsuresMemberType() - { - $manager = new Manager(); - $manager->addType((new VersionedStage())->toType()); - $manager->addType((new VersionSortType())->toType()); - $memberType = StaticSchema::inst()->typeNameForDataObject(Member::class); - $this->assertFalse($manager->hasType($memberType)); - - $scaffolder = new SchemaScaffolder(); - $scaffolder->type(Fake::class); - $scaffolder->addToManager($manager); - - $this->assertTrue($manager->hasType($memberType)); - $this->assertInstanceOf(ObjectType::class, $manager->getType($memberType)); - - $manager = new Manager(); - $this->assertFalse($manager->hasType($memberType)); - - $scaffolder = new SchemaScaffolder(); - $scaffolder->type(UnversionedWithField::class); - $scaffolder->addToManager($manager); - $this->assertFalse($manager->hasType($memberType)); - } -} diff --git a/tests/php/GraphQL/Legacy/Operations/CopyToStageTest.php b/tests/php/GraphQL/Legacy/Operations/CopyToStageTest.php deleted file mode 100644 index 7d555c60..00000000 --- a/tests/php/GraphQL/Legacy/Operations/CopyToStageTest.php +++ /dev/null @@ -1,116 +0,0 @@ -markTestSkipped('Skipped GraphQL 3 test ' . __CLASS__); - } - } - - - public function testCopyToStage() - { - $typeName = StaticSchema::inst()->typeNameForDataObject(Fake::class); - $manager = new Manager(); - $manager->addType((new CopyToStageInputType())->toType()); - $manager->addType(new ObjectType(['name' => $typeName])); - $copyToStage = new CopyToStage(Fake::class); - $scaffold = $copyToStage->scaffold($manager); - $this->assertIsCallable($scaffold['resolve']); - - /* @var Fake|Versioned $record */ - $record = new Fake(); - $record->Name = 'First'; - $record->write(); // v1 - - $this->logInWithPermission('ADMIN'); - $member = Security::getCurrentUser(); - $scaffold['resolve']( - null, - [ - 'Input' => [ - 'FromStage' => Versioned::DRAFT, - 'ToStage' => Versioned::LIVE, - 'ID' => $record->ID, - ], - ], - [ 'currentUser' => $member ], - new FakeResolveInfo() - ); - $recordLive = Versioned::get_by_stage(Fake::class, Versioned::LIVE) - ->byID($record->ID); - $this->assertNotNull($recordLive); - $this->assertEquals($record->ID, $recordLive->ID); - - $record->Name = 'Second'; - $record->write(); - $newVersion = $record->Version; - - $recordLive = Versioned::get_by_stage(Fake::class, Versioned::LIVE) - ->byID($record->ID); - $this->assertEquals('First', $recordLive->Title); - - // Invoke publish - $scaffold['resolve']( - null, - [ - 'Input' => [ - 'FromVersion' => $newVersion, - 'ToStage' => Versioned::LIVE, - 'ID' => $record->ID, - ], - ], - [ 'currentUser' => $member ], - new FakeResolveInfo() - ); - $recordLive = Versioned::get_by_stage(Fake::class, Versioned::LIVE) - ->byID($record->ID); - $this->assertEquals('Second', $recordLive->Title); - - // Test error - $this->expectException(\InvalidArgumentException::class); - $scaffold['resolve']( - null, - [ - 'Input' => [ - 'ToStage' => Versioned::DRAFT, - 'ID' => $record->ID, - ], - ], - [ 'currentUser' => new Member() ], - new FakeResolveInfo() - ); - } -} diff --git a/tests/php/GraphQL/Legacy/Operations/PublishTest.php b/tests/php/GraphQL/Legacy/Operations/PublishTest.php deleted file mode 100644 index 228a4d08..00000000 --- a/tests/php/GraphQL/Legacy/Operations/PublishTest.php +++ /dev/null @@ -1,86 +0,0 @@ -markTestSkipped('Skipped GraphQL 3 test ' . __CLASS__); - } - } - - public function testPublish() - { - $typeName = StaticSchema::inst()->typeNameForDataObject(Fake::class); - $manager = new Manager(); - $manager->addType(new ObjectType(['name' => $typeName])); - - $publish = new Publish(Fake::class); - $scaffold = $publish->scaffold($manager); - $this->assertIsCallable($scaffold['resolve']); - - $record = new Fake(); - $record->Name = 'First'; - $record->write(); - - $result = Versioned::get_by_stage(Fake::class, Versioned::LIVE) - ->byID($record->ID); - - $this->assertNull($result); - $this->logInWithPermission('ADMIN'); - $member = Security::getCurrentUser(); - $scaffold['resolve']( - null, - [ - 'ID' => $record->ID - ], - [ 'currentUser' => $member ], - new FakeResolveInfo() - ); - $result = Versioned::get_by_stage(Fake::class, Versioned::LIVE) - ->byID($record->ID); - - $this->assertNotNull($result); - $this->assertInstanceOf(Fake::class, $result); - $this->assertEquals('First', $result->Name); - - $this->expectException(Exception::class); - $this->expectExceptionMessageMatches('/^Not allowed/'); - $scaffold['resolve']( - null, - [ - 'ID' => $record->ID - ], - [ 'currentUser' => new Member() ], - new FakeResolveInfo() - ); - } -} diff --git a/tests/php/GraphQL/Legacy/Operations/ReadVersionsTest.php b/tests/php/GraphQL/Legacy/Operations/ReadVersionsTest.php deleted file mode 100644 index 911b4ff9..00000000 --- a/tests/php/GraphQL/Legacy/Operations/ReadVersionsTest.php +++ /dev/null @@ -1,134 +0,0 @@ -markTestSkipped('Skipped GraphQL 3 test ' . __CLASS__); - } - } - - - public function testItThrowsIfAppliedToAnUnversionedObject() - { - $manager = new Manager(); - $manager->addType((new VersionedStage())->toType()); - $manager->addType((new VersionSortType())->toType()); - $manager->addType(new ObjectType(['name' => 'Test'])); - $readVersions = new ReadVersions(UnversionedWithField::class, 'Test'); - $readVersions->setUsePagination(false); - $scaffold = $readVersions->scaffold($manager); - $this->assertIsCallable($scaffold['resolve']); - - $this->expectException(Exception::class); - $this->expectExceptionMessageMatches('/must have the Versioned extension/'); - $scaffold['resolve']( - new UnversionedWithField(), - [], - ['currentUser' => new Member()], - new FakeResolveInfo() - ); - } - - public function testItThrowsIfYouCantReadStages() - { - $manager = new Manager(); - $manager->addType((new VersionedStage())->toType()); - $manager->addType((new VersionSortType())->toType()); - $manager->addType(new ObjectType(['name' => 'Test'])); - $readVersions = new ReadVersions(Fake::class, 'Test'); - $readVersions->setUsePagination(false); - $scaffold = $readVersions->scaffold($manager); - $this->assertIsCallable($scaffold['resolve']); - - $this->expectException(Exception::class); - $this->expectExceptionMessageMatches('/Cannot view versions/'); - $scaffold['resolve']( - new Fake(), - [], - ['currentUser' => new Member()], - new FakeResolveInfo() - ); - } - - public function testItReadsVersions() - { - $manager = new Manager(); - $manager->addType((new VersionedStage())->toType()); - $manager->addType((new VersionSortType())->toType()); - $manager->addType(new ObjectType(['name' => 'Test'])); - $readVersions = new ReadVersions(Fake::class, 'Test'); - $readVersions->setUsePagination(false); - $scaffold = $readVersions->scaffold($manager); - $this->assertIsCallable($scaffold['resolve']); - $this->logInWithPermission('ADMIN'); - $member = Security::getCurrentUser(); - - $record = new Fake(); - $record->Name = 'First'; - $record->write(); - - $record->Name = 'Second'; - $record->write(); - - $record->Name = 'Third'; - $record->write(); - - $result = $scaffold['resolve']( - $record, - [], - ['currentUser' => $member], - new FakeResolveInfo() - ); - - $this->assertInstanceOf(SS_List::class, $result); - $this->assertCount(3, $result); - $this->assertInstanceOf(Fake::class, $result->first()); - $this->assertEquals(1, $result->first()->Version); - $this->assertEquals(3, $result->last()->Version); - } - - public function testVersionFieldIsSortable() - { - $operation = new ReadVersions(Fake::class, 'FakeClass'); - - // Omit the test if the API isn't available (must be running silverstripe-graphql < 3) - if (!method_exists($operation, 'getSortableFields')) { - $this->markTestSkipped('getSortableFields API is missing'); - } - $args = $operation->getArgs()->filter('argName', 'sort'); - $this->assertCount(1, $args); - } -} diff --git a/tests/php/GraphQL/Legacy/Operations/RollbackTest.php b/tests/php/GraphQL/Legacy/Operations/RollbackTest.php deleted file mode 100644 index 1585f9d7..00000000 --- a/tests/php/GraphQL/Legacy/Operations/RollbackTest.php +++ /dev/null @@ -1,91 +0,0 @@ -markTestSkipped('Skipped GraphQL 3 test ' . __CLASS__); - } - } - - public function testRollbackCannotBePerformedWithoutEditPermission() - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Current user does not have permission to roll back this resource'); - // Create a fake version of our stub - $stub = FakeDataObjectStub::create(); - $stub->Name = 'First'; - $stub->Editable = false; - $stub->write(); - - $this->doMutation($stub); - } - - public function testRollbackRecursiveIsCalled() - { - // Create a fake version of our stub - $stub = FakeDataObjectStub::create(); - $stub->Name = 'First'; - $stub->write(); - - $this->doMutation($stub); - - $this->assertTrue($stub::$rollbackCalled, 'RollbackRecursive was called'); - } - - protected function doMutation(DataObject $stub, $toVersion = 1, $member = null) - { - if (!$stub->isInDB()) { - $stub->write(); - } - - $stubClass = get_class($stub); - $typeName = StaticSchema::inst()->typeNameForDataObject($stubClass); - $manager = new Manager(); - $manager->addType(new ObjectType(['name' => $typeName])); - - $mutation = new Rollback($stubClass); - $scaffold = $mutation->scaffold($manager); - $this->assertIsCallable($scaffold['resolve'], 'Resolve function is scaffolded correctly'); - - $args = [ - 'ID' => $stub->ID, - 'ToVersion' => $toVersion, - ]; - - $scaffold['resolve']( - null, - $args, - [ 'currentUser' => $member ?: Security::getCurrentUser() ], - new FakeResolveInfo() - ); - } -} diff --git a/tests/php/GraphQL/Legacy/Operations/UnpublishTest.php b/tests/php/GraphQL/Legacy/Operations/UnpublishTest.php deleted file mode 100644 index 86600452..00000000 --- a/tests/php/GraphQL/Legacy/Operations/UnpublishTest.php +++ /dev/null @@ -1,89 +0,0 @@ -markTestSkipped('Skipped GraphQL 3 test ' . __CLASS__); - } - } - - public function testPublish() - { - $typeName = StaticSchema::inst()->typeNameForDataObject(Fake::class); - $manager = new Manager(); - $manager->addType(new ObjectType(['name' => $typeName])); - - $publish = new Unpublish(Fake::class); - $scaffold = $publish->scaffold($manager); - $this->assertIsCallable($scaffold['resolve']); - - $record = new Fake(); - $record->Name = 'First'; - $record->write(); - $record->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $result = Versioned::get_by_stage(Fake::class, Versioned::LIVE) - ->byID($record->ID); - - $this->assertNotNull($result); - $this->assertInstanceOf(Fake::class, $result); - $this->assertEquals('First', $result->Name); - - $this->logInWithPermission('ADMIN'); - $member = Security::getCurrentUser(); - $scaffold['resolve']( - null, - [ - 'ID' => $record->ID - ], - [ 'currentUser' => $member ], - new FakeResolveInfo() - ); - $result = Versioned::get_by_stage(Fake::class, Versioned::LIVE) - ->byID($record->ID); - - $this->assertNull($result); - - $record->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $this->expectException(Exception::class); - $this->expectExceptionMessageMatches('/^Not allowed/'); - $scaffold['resolve']( - null, - [ - 'ID' => $record->ID - ], - [ 'currentUser' => new Member() ], - new FakeResolveInfo() - ); - } -} diff --git a/tests/php/GraphQL/Legacy/Resolvers/ApplyVersionFiltersTest.php b/tests/php/GraphQL/Legacy/Resolvers/ApplyVersionFiltersTest.php deleted file mode 100644 index 7c879e4f..00000000 --- a/tests/php/GraphQL/Legacy/Resolvers/ApplyVersionFiltersTest.php +++ /dev/null @@ -1,291 +0,0 @@ -markTestSkipped('Skipped GraphQL 3 test ' . __CLASS__); - } - } - - public function testItValidatesArchiveDate() - { - $filter = new ApplyVersionFilters(); - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/ArchiveDate parameter/'); - $filter->validateArgs(['Mode' => 'archive']); - } - - public function testItValidatesArchiveDateFormat() - { - $filter = new ApplyVersionFilters(); - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Invalid date/'); - $filter->validateArgs(['Mode' => 'archive', 'ArchiveDate' => '01/12/2018']); - } - - public function testItValidatesStatusParameter() - { - $filter = new ApplyVersionFilters(); - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Status parameter/'); - $filter->validateArgs(['Mode' => 'status']); - } - - public function testItValidatesVersionParameter() - { - $filter = new ApplyVersionFilters(); - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Version parameter/'); - $filter->validateArgs(['Mode' => 'version']); - } - - public function testItSetsReadingStateByMode() - { - Versioned::withVersionedMode(function () { - $filter = new ApplyVersionFilters(); - $filter->applyToReadingState(['Mode' => Versioned::DRAFT]); - $this->assertEquals(Versioned::DRAFT, Versioned::get_stage()); - }); - } - - public function testItSetsReadingStateByArchiveDate() - { - Versioned::withVersionedMode(function () { - $filter = new ApplyVersionFilters(); - $filter->applyToReadingState(['Mode' => 'archive', 'ArchiveDate' => '2018-01-01']); - $this->assertEquals('2018-01-01', Versioned::current_archived_date()); - }); - } - - public function testItFiltersByStageOnApplyToList() - { - $filter = new ApplyVersionFilters(); - $record1 = new Fake(); - $record1->Name = 'First version draft'; - $record1->write(); - - $record2 = new Fake(); - $record2->Name = 'First version live'; - $record2->write(); - $record2->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - - $list = Fake::get(); - $filter->applyToList($list, ['Mode' => Versioned::DRAFT]); - $this->assertCount(2, $list); - - $list = Fake::get(); - $filter->applyToList($list, ['Mode' => Versioned::LIVE]); - $this->assertCount(1, $list); - } - - public function testItThrowsIfArchiveAndNoDateOnApplyToList() - { - $filter = new ApplyVersionFilters(); - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/ArchiveDate parameter/'); - $list = Fake::get(); - $filter->applyToList($list, ['Mode' => 'archive']); - } - - public function testItThrowsIfArchiveAndInvalidDateOnApplyToList() - { - $filter = new ApplyVersionFilters(); - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Invalid date/'); - $list = Fake::get(); - $filter->applyToList($list, ['Mode' => 'archive', 'ArchiveDate' => 'foo']); - } - - - public function testItThrowsIfVersionAndNoVersionOnApplyToList() - { - $filter = new ApplyVersionFilters(); - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Version parameter/'); - $list = Fake::get(); - $filter->applyToList($list, ['Mode' => 'version']); - } - - public function testItSetsArchiveQueryParamsOnApplyToList() - { - $filter = new ApplyVersionFilters(); - $list = Fake::get(); - $filter->applyToList( - $list, - [ - 'Mode' => 'archive', - 'ArchiveDate' => '2016-11-08', - ] - ); - - $this->assertEquals('archive', $list->dataQuery()->getQueryParam('Versioned.mode')); - $this->assertEquals('2016-11-08', $list->dataQuery()->getQueryParam('Versioned.date')); - } - - public function testItSetsVersionQueryParamsOnApplyToList() - { - $filter = new ApplyVersionFilters(); - $list = Fake::get(); - $filter->applyToList( - $list, - [ - 'Mode' => 'version', - 'Version' => '5', - ] - ); - - $this->assertEquals('version', $list->dataQuery()->getQueryParam('Versioned.mode')); - $this->assertEquals('5', $list->dataQuery()->getQueryParam('Versioned.version')); - } - - public function testItSetsLatestVersionQueryParamsOnApplyToList() - { - $filter = new ApplyVersionFilters(); - $list = Fake::get(); - $filter->applyToList( - $list, - [ - 'Mode' => 'latest_versions', - ] - ); - - $this->assertEquals('latest_versions', $list->dataQuery()->getQueryParam('Versioned.mode')); - } - - public function testItSetsAllVersionsQueryParamsOnApplyToList() - { - $filter = new ApplyVersionFilters(); - $list = Fake::get(); - $filter->applyToList( - $list, - [ - 'Mode' => 'all_versions', - ] - ); - - $this->assertEquals('all_versions', $list->dataQuery()->getQueryParam('Versioned.mode')); - } - - public function testItThrowsOnNoStatusOnApplyToList() - { - $filter = new ApplyVersionFilters(); - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessageMatches('/Status parameter/'); - $list = Fake::get(); - $filter->applyToList($list, ['Mode' => 'status']); - } - - public function testStatusOnApplyToList() - { - $filter = new ApplyVersionFilters(); - $record1 = new Fake(); - $record1->Name = 'Only on draft'; - $record1->write(); - - $record2 = new Fake(); - $record2->Name = 'Published'; - $record2->write(); - $record2->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - - $record3 = new Fake(); - $record2->Name = 'Will be modified'; - $record3->write(); - $record3->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $record3->Name = 'Modified'; - $record3->write(); - - $record4 = new Fake(); - $record4->Name = 'Will be archived'; - $record4->write(); - $record4->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); - $oldID = $record4->ID; - $record4->delete(); - $list = Fake::get(); - $filter->applyToList( - $list, - [ - 'Mode' => 'status', - 'Status' => ['modified'] - ] - ); - $this->assertListEquals([['ID' => $record3->ID]], $list); - - $list = Fake::get(); - $filter->applyToList( - $list, - [ - 'Mode' => 'status', - 'Status' => ['archived'] - ] - ); - $this->assertCount(1, $list); - $this->assertEquals($oldID, $list->first()->ID); - - $list = Fake::get(); - $filter->applyToList( - $list, - [ - 'Mode' => 'status', - 'Status' => ['draft'] - ] - ); - - $this->assertCount(1, $list); - $ids = $list->column('ID'); - $this->assertContains($record1->ID, $ids); - - - $list = Fake::get(); - $filter->applyToList( - $list, - [ - 'Mode' => 'status', - 'Status' => ['draft', 'modified'] - ] - ); - - $this->assertCount(2, $list); - $ids = $list->column('ID'); - - $this->assertContains($record3->ID, $ids); - $this->assertContains($record1->ID, $ids); - - $list = Fake::get(); - $filter->applyToList( - $list, - [ - 'Mode' => 'status', - 'Status' => ['archived', 'modified'] - ] - ); - - $this->assertCount(2, $list); - $ids = $list->column('ID'); - $this->assertTrue(in_array($record3->ID, $ids ?? [])); - $this->assertTrue(in_array($oldID, $ids ?? [])); - } -} diff --git a/tests/php/GraphQL/Plugins/VersionedReadTest.php b/tests/php/GraphQL/Plugins/VersionedReadTest.php index 699c27bb..61b7fc01 100644 --- a/tests/php/GraphQL/Plugins/VersionedReadTest.php +++ b/tests/php/GraphQL/Plugins/VersionedReadTest.php @@ -1,6 +1,6 @@