Completed
Push — master ( ee1081...62ba87 )
by Werner
12:24
created

RootFolderTest::testDuplicatePage()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
/**
4
 * Tests for RootFolder extension
5
 */
6
class RootFolderTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
9
    protected static $fixture_file = 'RootFolderTest.yml';
10
11
    /**
12
     * Check if a folder is generated and saved when a page is saved.
13
     */
14
    public function testCreateFolder()
15
    {
16
        $page = Page::create();
17
        $this->assertEquals(0, $page->RootFolderID, 'a new Page should not have a folder yet');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
18
19
        $page->Title = 'Create Page Test';
20
21
        $page->write();
22
23
        $this->assertNotEquals(0, $page->RootFolderID, 'a page should have a folder after saving');
0 ignored issues
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
25
        $folder = $page->RootFolder();
26
27
        $this->assertEquals($page->URLSegment, $folder->Name, 'Page URLSegment and Folder Title should be the same');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
        $path = ASSETS_DIR . '/' . $root = Config::inst()->get('RootFolder', 'folder_root') . '/'
29
                . $page->URLSegment . '/';
30
31
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
            $path,
33
            $folder->getRelativePath(),
34
            'folder path should be assets/Articles/' . $page->URLSegment
35
        );
36
    }
37
38
    /**
39
     * Checks if the folder will be updated when saving a page and changing the URLSegment
40
     */
41
    public function testUpdateFolder()
42
    {
43
        $page1 = $this->objFromFixture('Page', 'page1');
44
        $folder = $page1->RootFolder();
45
46
        $this->assertEquals($page1->URLSegment, $folder->Name, 'Page URLSegment and Folder Title should be the same');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
48
        $page1->URLSegment = ('updatedpage');
49
        $page1->write();
50
51
        $folder = $page1->RootFolder(); //reload folder after saving
52
        $this->assertEquals('updatedpage', $folder->Name, 'Folder name should be updated after saving a page');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
            $page1->URLSegment,
55
            $folder->Name,
56
            'Page URLSegment and Folder Title should be the same, even after updating'
57
        );
58
    }
59
60
    /**
61
     * Checks if no folder is created for ignored page types, e.g. VirtualPage or ErrorPage
62
     */
63
    public function testIgnoredPageTypes()
64
    {
65
        $ignoredPageTypes = Config::inst()->get('RootFolder', 'ignored_classes');
66
67
        foreach ($ignoredPageTypes as $type) {
0 ignored issues
show
Bug introduced by
The expression $ignoredPageTypes of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
68
            $page = $type::create();
69
70
            $page->write();
71
            $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
                0,
73
                $page->RootFolderID,
74
                'Ignored page type ' . $type . ' should not have a RootFolderID'
75
            );
76
        }
77
    }
78
79
    /**
80
     * Check if subpage's folder is a subfolder of parent page
81
     */
82
    public function testHierarchy()
83
    {
84
        $parent = $this->objFromFixture('Page', 'parentpage');
85
        $child = $this->objFromFixture('Page', 'subpage');
86
87
        //test if fixtures are set up properly
88
        $this->assertEquals($parent->ID, $child->ParentID, 'subpage should be a child of parentpage');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
90
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
            $parent->RootFolderID,
92
            $child->RootFolder()->ParentID,
93
            'rootfolder2 should be a child of rootfolder 1'
94
        );
95
96
        $newPage = Page::create();
97
        $newPage->ParentID = $parent->ID;
98
        $newPage->Title = 'Hierarchy Test';
99
        $newPage->urlSegment = 'hierarchy-test';
100
        $newPage->write();
101
102
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
            $parent->RootFolderID,
104
            $newPage->RootFolder()->ParentID,
105
            'new folder should be a child of page1 folder'
106
        );
107
    }
108
109
    /**
110
     * Checks if getRootFolderName() works properly
111
     */
112
    public function testGetRootFolderName()
113
    {
114
        $parent = $this->objFromFixture('Page', 'parentpage');
115
        $child = $this->objFromFixture('Page', 'subpage');
116
117
        //test if fixtures are set up properly
118
        $this->assertEquals($parent->ID, $child->ParentID, 'subpage should be a child of parentpage');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
120
        $this->assertStringEndsWith(
0 ignored issues
show
Bug introduced by
The method assertStringEndsWith() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
            $child->RootFolder()->Name . '/',
122
            $child->getRootFolderName(),
123
            'FolderName should be at the end of getRootFolderName()'
124
        );
125
126
        $root = Config::inst()->get('RootFolder', 'folder_root');
127
        $this->assertStringStartsWith(
0 ignored issues
show
Bug introduced by
The method assertStringStartsWith() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
            $root . '/' . $parent->RootFolder()->Name,
129
            $child->getRootFolderName(),
130
            'Parents FolderName should be at the beginning of getRootFolderName()'
131
        );
132
133
        $this->assertStringStartsWith(
0 ignored issues
show
Bug introduced by
The method assertStringStartsWith() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
            ASSETS_DIR,
135
            $child->getRootFolderName(false),
136
            'ASSETS_DIR should be at the beginning of getRootFolderName(false)'
137
        );
138
    }
139
140
    /**
141
     * Check if a duplicated page has a new folder applied
142
     */
143
    public function testDuplicatePage()
144
    {
145
        $page = $this->objFromFixture('Page', 'page1');
146
147
        $this->assertFileExists($page->RootFolder()->getFullPath(),
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
            'root folder of original page should exist on file system');
149
150
        $duplicatedPage = $page->duplicate(true);
151
152
        //we have to re-load the duplicated page, cause the RootFolder is set in onAfterWrite and the current
153
        //object is not aware of it
154
155
        $duplicatedPage = Page::get()->byID($duplicatedPage->ID);
156
157
        $this->assertNotEquals(
0 ignored issues
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
158
            $page->URLSegment,
159
            $duplicatedPage->URLSegment,
160
            'The duplicated page must not have the same urlsegment'
161
        );
162
        $this->assertNotEquals(
0 ignored issues
show
Bug introduced by
The method assertNotEquals() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
163
            $page->RootFolderID,
164
            $duplicatedPage->RootFolderID,
165
            'The duplicated page must not have the same root folder'
166
        );
167
168
        $this->assertFileExists($page->RootFolder()->getFullPath(),
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
            'root folder of original page should still exist on file system after duplication');
170
        $this->assertFileExists($duplicatedPage->RootFolder()->getFullPath(),
0 ignored issues
show
Bug introduced by
The method assertFileExists() does not seem to exist on object<RootFolderTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
171
            'root folder of duplicated page should exist on file system');
172
173
    }
174
175
}
176