|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\AssetAdmin\Tests\GraphQL; |
|
4
|
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
6
|
|
|
use SilverStripe\AssetAdmin\GraphQL\Notice; |
|
7
|
|
|
use SilverStripe\AssetAdmin\GraphQL\UnpublishFileMutationCreator; |
|
8
|
|
|
use SilverStripe\Assets\File; |
|
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
10
|
|
|
use SilverStripe\Security\Security; |
|
11
|
|
|
|
|
12
|
|
|
class UnpublishFileMutationCreatorTest extends SapphireTest |
|
13
|
|
|
{ |
|
14
|
|
|
protected static $fixture_file = 'UnpublishFileMutationCreatorTest.yml'; |
|
15
|
|
|
|
|
16
|
|
|
protected static $extra_dataobjects = [ |
|
17
|
|
|
UnpublishFileMutationCreatorTest\FileOwner::class, |
|
18
|
|
|
]; |
|
19
|
|
|
|
|
20
|
|
|
protected function setUp() : void |
|
21
|
|
|
{ |
|
22
|
|
|
parent::setUp(); |
|
23
|
|
|
|
|
24
|
|
|
// Dynamically assign fileowner as owner (otherwise it pollutes other tests) |
|
25
|
|
|
UnpublishFileMutationCreatorTest\FileOwner::config()->set('owns', ['OwnedFile']); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testUnpublishWithOwners() |
|
29
|
|
|
{ |
|
30
|
|
|
// Bootstrap test |
|
31
|
|
|
$this->logInWithPermission('ADMIN'); |
|
32
|
|
|
$member = Security::getCurrentUser(); |
|
33
|
|
|
$mutation = new UnpublishFileMutationCreator(); |
|
34
|
|
|
$context = ['currentUser' => $member]; |
|
35
|
|
|
$resolveInfo = new ResolveInfo([]); |
|
36
|
|
|
|
|
37
|
|
|
/** @var File $file */ |
|
38
|
|
|
$file = $this->objFromFixture(File::class, 'file1'); |
|
39
|
|
|
$file->publishSingle(); |
|
40
|
|
|
|
|
41
|
|
|
// 4 owners, 3 published owners |
|
42
|
|
|
for ($i = 1; $i <= 4; $i++) { |
|
43
|
|
|
$owner = new UnpublishFileMutationCreatorTest\FileOwner(); |
|
44
|
|
|
$owner->OwnedFileID = $file->ID; |
|
45
|
|
|
$owner->Title = "My Owner {$i}"; |
|
46
|
|
|
$owner->write(); |
|
47
|
|
|
// Only 3 of these are published |
|
48
|
|
|
if ($i !== 4) { |
|
49
|
|
|
$owner->publishSingle(); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Test unpublish without force |
|
54
|
|
|
$result = $mutation->resolve(null, ['IDs' => [$file->ID]], $context, $resolveInfo); |
|
55
|
|
|
$this->assertCount(1, $result); |
|
56
|
|
|
/** @var Notice $notice */ |
|
57
|
|
|
$notice = $result[0]; |
|
58
|
|
|
$this->assertInstanceOf(Notice::class, $notice); |
|
59
|
|
|
$this->assertEquals('File "The First File" is used in 3 places.', $notice->getMessage()); |
|
60
|
|
|
$this->assertTrue($file->isPublished()); |
|
61
|
|
|
|
|
62
|
|
|
// Unpublish with force |
|
63
|
|
|
$result = $mutation->resolve(null, ['IDs' => [$file->ID], 'Force' => true], $context, $resolveInfo); |
|
64
|
|
|
$this->assertCount(1, $result); |
|
65
|
|
|
$fileResult = $result[0]; |
|
66
|
|
|
$this->assertInstanceOf(File::class, $fileResult); |
|
67
|
|
|
$this->assertFalse($file->isPublished()); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|