-
Notifications
You must be signed in to change notification settings - Fork 10
/
SiteTreeImporterTest.php
172 lines (148 loc) · 5.43 KB
/
SiteTreeImporterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
class SiteTreeImporterTest extends FunctionalTest
{
protected $extraDataObjects = array(
'Page' // Hack to get SapphireTest refreshing the DB
);
protected static $use_draft_site = true;
public function testImport()
{
// create sample record
$page = new Page();
$page->Title = 'ShouldBeExisting';
$page->write();
$data = <<<YML
# Comments are skipped
Parent1
Parent2
Child2_1
Grandchild2_1_1
Child2_2
Parent3
YML;
$this->import($data);
$existing = Page::get()->find('Title', 'ShouldBeExisting');
$parent1 = Page::get()->find('Title', 'Parent1');
$parent2 = Page::get()->find('Title', 'Parent2');
$parent3 = Page::get()->find('Title', 'Parent3');
$child2_1 = Page::get()->find('Title', 'Child2_1');
$child2_2 = Page::get()->find('Title', 'Child2_2');
$grandchild2_1_1 = Page::get()->find('Title', 'Grandchild2_1_1');
$this->assertInstanceOf('Page', $existing);
$this->assertInstanceOf('Page', $parent1);
$this->assertInstanceOf('Page', $parent2);
$this->assertInstanceOf('Page', $parent3);
$this->assertInstanceOf('Page', $child2_1);
$this->assertInstanceOf('Page', $child2_2);
$this->assertInstanceOf('Page', $grandchild2_1_1);
$this->assertEquals($parent2->ID, $child2_1->ParentID);
$this->assertEquals($parent2->ID, $child2_2->ParentID);
$this->assertEquals($child2_1->ID, $grandchild2_1_1->ParentID);
}
public function testImportPublishAll()
{
$data = <<<YML
Parent1
Parent2
Child2_1
Grandchild2_1_1
Child2_2
Parent3
YML;
$this->import($data, array('PublishAll' => 1));
$parent1 = Versioned::get_one_by_stage('Page', 'Live', "\"Title\" = 'Parent1'");
$parent2 = Versioned::get_one_by_stage('Page', 'Live', "\"Title\" = 'Parent2'");
$parent3 = Versioned::get_one_by_stage('Page', 'Live', "\"Title\" = 'Parent3'");
$child2_1 = Versioned::get_one_by_stage('Page', 'Live', "\"Title\" = 'Child2_1'");
$child2_2 = Versioned::get_one_by_stage('Page', 'Live', "\"Title\" = 'Child2_2'");
$grandchild2_1_1 = Versioned::get_one_by_stage('Page', 'Live', "\"Title\" = 'Grandchild2_1_1'");
$this->assertInstanceOf('Page', $parent1);
$this->assertInstanceOf('Page', $parent2);
$this->assertInstanceOf('Page', $parent3);
$this->assertInstanceOf('Page', $child2_1);
$this->assertInstanceOf('Page', $child2_2);
$this->assertInstanceOf('Page', $grandchild2_1_1);
}
public function testImportDeleteExisting()
{
// create sample record
$page = new Page();
$page->Title = 'ShouldBeDeleted';
$page->write();
$data = <<<YML
Parent1
YML;
$this->import($data, array('DeleteExisting' => 1));
$this->assertInstanceOf('Page', Page::get()->find('Title', 'Parent1'));
$this->assertNull(Page::get()->find('Title', 'ShouldBeDeleted'));
}
public function testImportSkipsComments()
{
$data = <<<YML
# Comments are skipped
Parent1
YML;
$this->import($data);
$this->assertNull(Page::get()->find('Title', '# Comments are skipped'));
}
public function testImportWithJSON()
{
// create sample record
$page = new Page();
$page->Title = 'ShouldBeExisting';
$page->URLSegment = 'existing';
$page->write();
$page->publish('Stage', 'Live');
$data = <<<YML
Parent1
Parent2
Child2_1
Grandchild2_1_1
Child2_2 {"URLSegment": "my-url", "MenuTitle": "my-menu-title", "ClassName": "SiteTreeImporterTest_TestPage"}
Parent3
YML;
$this->import($data);
$child2_2 = Page::get()->find('Title', 'Child2_2');
$this->assertInstanceOf('Page', $child2_2);
$this->assertEquals($child2_2->URLSegment, 'my-url');
$this->assertEquals($child2_2->MenuTitle, 'my-menu-title');
$this->assertEquals($child2_2->ClassName, 'SiteTreeImporterTest_TestPage');
}
public function testDetectsDuplicatesByParent()
{
$page = new Page();
$page->Title = 'MyPage';
$page->URLSegment = 'mypage';
$page->write();
$page->publish('Stage', 'Live');
// Import page with same name but a different hierarchy
$data = <<<YML
Parent
MyPage
YML;
$this->import($data);
$pages = Page::get()->filter('Title', 'MyPage')->sort('Created');
$existingPage = $pages->filter('ID', $page->ID)->First();
$this->assertNotNull($existingPage);
$this->assertEquals($existingPage->Title, 'MyPage');
$this->assertEquals((int)$existingPage->ParentID, (int)$page->ParentID);
$newPage = $pages->exclude('ID', $page->ID)->First();
$this->assertNotNull($newPage);
$this->assertEquals($newPage->Title, 'MyPage');
$this->assertNotEquals((int)$newPage->ParentID, (int)$page->ParentID);
}
protected function import($yml, $data = null)
{
$data = $data ? $data : array();
$data['SourceFile'] = array();
$tmpFile = tempnam('SiteTreeImporterTest', 'SiteTreeImporterTest');
file_put_contents($tmpFile, $yml);
$data['SourceFile']['tmp_name'] = $tmpFile;
$importer = new SiteTreeImporter();
$response = $importer->bulkimport($data, null);
unlink($tmpFile);
}
}
class SiteTreeImporterTest_TestPage extends Page implements HiddenClass
{
}