1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NetWerkstatt\FolderPerPage\Tests; |
4
|
|
|
|
5
|
|
|
use Page; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use NetWerkstatt\FolderPerPage\Extensions\RootFolder; |
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Tests for RootFolder extension |
12
|
|
|
*/ |
13
|
|
|
class RootFolderTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
protected static $fixture_file = 'RootFolderTest.yml'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Check if a folder is generated and saved when a page is saved. |
20
|
|
|
*/ |
21
|
|
|
public function testCreateFolder() |
22
|
|
|
{ |
23
|
|
|
$page = Page::create(); |
24
|
|
|
$this->assertEquals(0, $page->RootFolderID, 'a new Page should not have a folder yet'); |
25
|
|
|
|
26
|
|
|
$page->Title = 'Create Page Test'; |
27
|
|
|
|
28
|
|
|
$page->write(); |
29
|
|
|
|
30
|
|
|
$this->assertNotEquals(0, $page->RootFolderID, 'a page should have a folder after saving'); |
31
|
|
|
|
32
|
|
|
$folder = $page->RootFolder(); |
33
|
|
|
|
34
|
|
|
$this->assertEquals($page->URLSegment, $folder->Name, 'Page URLSegment and Folder Title should be the same'); |
35
|
|
|
$path = $root = Config::inst()->get(RootFolder::class, 'folder_root') . '/' |
|
|
|
|
36
|
|
|
. $page->URLSegment . '/'; |
37
|
|
|
|
38
|
|
|
$this->assertEquals( |
39
|
|
|
'create-page-test', |
40
|
|
|
$folder->getTitle(), |
41
|
|
|
'folder should be named "create-page-test"' |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$parentFolder = $folder->Parent(); |
45
|
|
|
$this->assertEquals( |
46
|
|
|
Config::inst()->get(RootFolder::class, 'folder_root'), |
47
|
|
|
$parentFolder->getTitle(), |
48
|
|
|
'parent folder should be folder_root config' |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Checks if the folder will be updated when saving a page and changing the URLSegment |
54
|
|
|
*/ |
55
|
|
|
public function testUpdateFolder() |
56
|
|
|
{ |
57
|
|
|
$page1 = $this->objFromFixture('Page', 'page1'); |
58
|
|
|
$folder = $page1->RootFolder(); |
59
|
|
|
|
60
|
|
|
$this->assertEquals( |
61
|
|
|
$page1->URLSegment, |
62
|
|
|
$folder->getTitle(), |
63
|
|
|
'Page URLSegment and Folder Title should be the same' |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$page1->URLSegment = 'updatedpage'; |
67
|
|
|
$page1->write(); |
68
|
|
|
|
69
|
|
|
$folder = $page1->RootFolder(); //reload folder after saving |
70
|
|
|
$this->assertEquals('updatedpage', $folder->getTitle(), 'Folder name should be updated after saving a page'); |
71
|
|
|
$this->assertEquals( |
72
|
|
|
$page1->URLSegment, |
73
|
|
|
$folder->Name, |
74
|
|
|
'Page URLSegment and Folder Title should be the same, even after updating' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Checks if no folder is created for ignored page types, e.g. VirtualPage or ErrorPage |
80
|
|
|
*/ |
81
|
|
|
public function testIgnoredPageTypes() |
82
|
|
|
{ |
83
|
|
|
$ignoredPageTypes = Config::inst()->get(RootFolder::class, 'ignored_classes'); |
84
|
|
|
|
85
|
|
|
foreach ($ignoredPageTypes as $type) { |
86
|
|
|
$page = $type::create(); |
87
|
|
|
|
88
|
|
|
$page->write(); |
89
|
|
|
$this->assertEquals( |
90
|
|
|
0, |
91
|
|
|
$page->RootFolderID, |
92
|
|
|
'Ignored page type ' . $type . ' should not have a RootFolderID' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check if subpage's folder is a subfolder of parent page |
99
|
|
|
*/ |
100
|
|
|
public function testHierarchy() |
101
|
|
|
{ |
102
|
|
|
$parent = $this->objFromFixture('Page', 'parentpage'); |
103
|
|
|
$child = $this->objFromFixture('Page', 'subpage'); |
104
|
|
|
|
105
|
|
|
//test if fixtures are set up properly |
106
|
|
|
$this->assertEquals($parent->ID, $child->ParentID, 'subpage should be a child of parentpage'); |
107
|
|
|
|
108
|
|
|
$this->assertEquals( |
109
|
|
|
$parent->RootFolderID, |
110
|
|
|
$child->RootFolder()->ParentID, |
111
|
|
|
'rootfolder2 should be a child of rootfolder 1' |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$newPage = Page::create(); |
115
|
|
|
$newPage->ParentID = $parent->ID; |
116
|
|
|
$newPage->Title = 'Hierarchy Test'; |
117
|
|
|
$newPage->urlSegment = 'hierarchy-test'; |
118
|
|
|
$newPage->write(); |
119
|
|
|
|
120
|
|
|
$this->assertEquals( |
121
|
|
|
$parent->RootFolderID, |
122
|
|
|
$newPage->RootFolder()->ParentID, |
123
|
|
|
'new folder should be a child of page1 folder' |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Checks if getRootFolderName() works properly |
129
|
|
|
*/ |
130
|
|
|
public function testGetRootFolderName() |
131
|
|
|
{ |
132
|
|
|
$parent = $this->objFromFixture('Page', 'parentpage'); |
133
|
|
|
$child = $this->objFromFixture('Page', 'subpage'); |
134
|
|
|
|
135
|
|
|
//test if fixtures are set up properly |
136
|
|
|
$this->assertEquals($parent->ID, $child->ParentID, 'subpage should be a child of parentpage'); |
137
|
|
|
|
138
|
|
|
$this->assertStringEndsWith( |
139
|
|
|
$child->RootFolder()->Name . '/', |
140
|
|
|
$child->getRootFolderName(), |
141
|
|
|
'FolderName should be at the end of getRootFolderName()' |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$root = Config::inst()->get(RootFolder::class, 'folder_root'); |
145
|
|
|
$this->assertStringStartsWith( |
146
|
|
|
$root . '/' . $parent->RootFolder()->Name, |
147
|
|
|
$child->getRootFolderName(), |
148
|
|
|
'Parents FolderName should be at the beginning of getRootFolderName()' |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
$this->assertStringStartsWith( |
152
|
|
|
ASSETS_DIR, |
153
|
|
|
$child->getRootFolderName(false), |
154
|
|
|
'ASSETS_DIR should be at the beginning of getRootFolderName(false)' |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Check if a duplicated page has a new folder applied |
160
|
|
|
*/ |
161
|
|
|
public function testDuplicatePage() |
162
|
|
|
{ |
163
|
|
|
$page = $this->objFromFixture('Page', 'page1'); |
164
|
|
|
|
165
|
|
|
// $this->assertFileExists($page->RootFolder()->getFullPath(), |
166
|
|
|
// 'root folder of original page should exist on file system'); |
167
|
|
|
|
168
|
|
|
$duplicatedPage = $page->duplicate(true); |
169
|
|
|
|
170
|
|
|
//we have to re-load the duplicated page, cause the RootFolder is set in onAfterWrite and the current |
171
|
|
|
//object is not aware of it |
172
|
|
|
|
173
|
|
|
$duplicatedPage = Page::get()->byID($duplicatedPage->ID); |
174
|
|
|
|
175
|
|
|
$this->assertNotEquals( |
176
|
|
|
$page->URLSegment, |
177
|
|
|
$duplicatedPage->URLSegment, |
178
|
|
|
'The duplicated page must not have the same urlsegment' |
179
|
|
|
); |
180
|
|
|
$this->assertNotEquals( |
181
|
|
|
$page->RootFolderID, |
182
|
|
|
$duplicatedPage->RootFolderID, |
183
|
|
|
'The duplicated page must not have the same root folder' |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
// $this->assertFileExists($page->RootFolder()->getFullPath(), |
187
|
|
|
// 'root folder of original page should still exist on file system after duplication'); |
188
|
|
|
// $this->assertFileExists($duplicatedPage->RootFolder()->getFullPath(), |
189
|
|
|
// 'root folder of duplicated page should exist on file system'); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.