UnpublishFileMutationCreatorTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 56
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testUnpublishWithOwners() 0 40 3
A setUp() 0 6 1
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();
0 ignored issues
show
Bug introduced by
The method publishSingle() does not exist on SilverStripe\AssetAdmin\...onCreatorTest\FileOwner. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
                $owner->/** @scrutinizer ignore-call */ 
50
                        publishSingle();
Loading history...
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