1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Tests for RootFolder extension |
5
|
|
|
*/ |
6
|
|
|
class RootFolderTest extends SapphireTest |
|
|
|
|
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'); |
|
|
|
|
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'); |
|
|
|
|
24
|
|
|
|
25
|
|
|
$folder = $page->RootFolder(); |
26
|
|
|
|
27
|
|
|
$this->assertEquals($page->URLSegment, $folder->Name, 'Page URLSegment and Folder Title should be the same'); |
|
|
|
|
28
|
|
|
$path = ASSETS_DIR . '/' . $root = Config::inst()->get('RootFolder', 'folder_root') . '/' |
29
|
|
|
. $page->URLSegment . '/'; |
30
|
|
|
|
31
|
|
|
$this->assertEquals( |
|
|
|
|
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'); |
|
|
|
|
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'); |
|
|
|
|
53
|
|
|
$this->assertEquals( |
|
|
|
|
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) { |
|
|
|
|
68
|
|
|
$page = $type::create(); |
69
|
|
|
|
70
|
|
|
$page->write(); |
71
|
|
|
$this->assertEquals( |
|
|
|
|
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'); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$this->assertEquals( |
|
|
|
|
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( |
|
|
|
|
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'); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$this->assertStringEndsWith( |
|
|
|
|
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( |
|
|
|
|
128
|
|
|
$root . '/' . $parent->RootFolder()->Name, |
129
|
|
|
$child->getRootFolderName(), |
130
|
|
|
'Parents FolderName should be at the beginning of getRootFolderName()' |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
$this->assertStringStartsWith( |
|
|
|
|
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(), |
|
|
|
|
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( |
|
|
|
|
158
|
|
|
$page->URLSegment, |
159
|
|
|
$duplicatedPage->URLSegment, |
160
|
|
|
'The duplicated page must not have the same urlsegment' |
161
|
|
|
); |
162
|
|
|
$this->assertNotEquals( |
|
|
|
|
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(), |
|
|
|
|
169
|
|
|
'root folder of original page should still exist on file system after duplication'); |
170
|
|
|
$this->assertFileExists($duplicatedPage->RootFolder()->getFullPath(), |
|
|
|
|
171
|
|
|
'root folder of duplicated page should exist on file system'); |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.