Completed
Pull Request — master (#53)
by Helpful
02:20
created

StaticPublisherTestPage::canPublish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Tests for the {@link FilesystemPublisher} class.
5
 * 
6
 * @package staticpublisher
7
 */
8
class FilesystemPublisherTest 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...
9
	
10
	protected $usesDatabase = true;
11
12
	public function setUp() {
13
		parent::setUp();
14
		
15
		SiteTree::add_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/')");
16
		
17
		Config::inst()->nest();
18
		Config::inst()->update('StaticPagesQueue', 'realtime', true);
19
		Config::inst()->update('FilesystemPublisher', 'domain_based_caching', false);
20
		Config::inst()->update('FilesystemPublisher', 'static_base_url', 'http://foo');
21
		Config::inst()->update('Director', 'alternate_base_url', 'http://foo/');
22
	}
23
	
24
	public function tearDown() {
25
		parent::tearDown();
26
27
		Config::inst()->unnest();
28
29
		SiteTree::remove_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/')");
30
31
		if(file_exists(BASE_PATH . '/assets/FilesystemPublisherTest-static-folder')) {
32
			Filesystem::removeFolder(BASE_PATH . '/assets/FilesystemPublisherTest-static-folder');
33
		}
34
35
		// Purge DB from StaticPagesQueue items.
36
		self::empty_temp_db();
37
	}
38
	
39
	public function testUrlsToPathsWithRelativeUrls() {
40
		$fsp = new FilesystemPublisher('.', 'html');
41
		
42
		$this->assertEquals(
43
			$fsp->urlsToPaths(array('/')),
44
			array('/' => './index.html'),
45
			'Root URL path mapping'
46
		);
47
		
48
		$this->assertEquals(
49
			$fsp->urlsToPaths(array('about-us')),
50
			array('about-us' => './about-us.html'),
51
			'URLsegment path mapping'
52
		);
53
		
54
		$this->assertEquals(
55
			$fsp->urlsToPaths(array('parent/child')),
56
			array('parent/child' => 'parent/child.html'),
57
			'Nested URLsegment path mapping'
58
		);
59
	}
60
	
61
	public function testUrlsToPathsWithAbsoluteUrls() {
62
		$fsp = new FilesystemPublisher('.', 'html');
63
		
64
		$url = Director::absoluteBaseUrl();
65
		$this->assertEquals(
66
			$fsp->urlsToPaths(array($url)),
67
			array($url => './index.html'),
68
			'Root URL path mapping'
69
		);
70
		
71
		$url = Director::absoluteBaseUrl() . 'about-us';
72
		$this->assertEquals(
73
			$fsp->urlsToPaths(array($url)),
74
			array($url => './about-us.html'),
75
			'URLsegment path mapping'
76
		);
77
		
78
		$url = Director::absoluteBaseUrl() . 'parent/child';
79
		$this->assertEquals(
80
			$fsp->urlsToPaths(array($url)),
81
			array($url => 'parent/child.html'),
82
			'Nested URLsegment path mapping'
83
		);
84
	}
85
86
	public function testUrlsToPathsWithDomainBasedCaching() {
87
		$origDomainBasedCaching = Config::inst()->get('FilesystemPublisher', 'domain_based_caching');
88
		Config::inst()->update('FilesystemPublisher', 'domain_based_caching', true);
89
		
90
		$fsp = new FilesystemPublisher('.', 'html');
91
		
92
		$url = 'http://domain1.com/';
93
		$this->assertEquals(
94
			$fsp->urlsToPaths(array($url)),
95
			array($url => 'domain1.com/index.html'),
96
			'Root URL path mapping'
97
		);
98
		
99
		$url = 'http://domain1.com/about-us';
100
		$this->assertEquals(
101
			$fsp->urlsToPaths(array($url)),
102
			array($url => 'domain1.com/about-us.html'),
103
			'URLsegment path mapping'
104
		);
105
		
106
		$url = 'http://domain2.com/parent/child';
107
		$this->assertEquals(
108
			$fsp->urlsToPaths(array($url)),
109
			array($url => 'domain2.com/parent/child.html'),
110
			'Nested URLsegment path mapping'
111
		);
112
		
113
		Config::inst()->update('FilesystemPublisher', 'domain_based_caching', $origDomainBasedCaching);
114
	}
115
	
116
	/**
117
	 * Simple test to ensure that FileSystemPublisher::__construct()
118
	 * has called parent::__construct() by checking the class property.
119
	 * The class property is set on {@link Object::__construct()} and
120
	 * this is therefore a good test to ensure it was called.
121
	 * 
122
	 * If FilesystemPublisher doesn't call parent::__construct() then
123
	 * it won't be enabled propery because {@link Object::__construct()}
124
	 * is where extension instances are set up and subsequently used by
125
	 * {@link DataObject::defineMethods()}.
126
	 */
127
	public function testHasCalledParentConstructor() {
128
		$fsp = new FilesystemPublisher('.', '.html');
129
130
		$this->assertEquals($fsp->class, 'FilesystemPublisher');
131
	}
132
	
133
	/*
134
	 * These are a few simple tests to check that we will be retrieving the 
135
	 * correct theme when we need it. StaticPublishing needs to be able to 
136
	 * retrieve a non-null theme at the time publishPages() is called.
137
	 */
138
	public function testStaticPublisherTheme(){
139
		
140
		// This will be the name of the default theme of this particular project
141
		$default_theme = Config::inst()->get('SSViewer', 'theme');
142
		
143
		$p1 = new Page();
144
		$p1->URLSegment = strtolower(__CLASS__).'-page-1';
145
		$p1->HomepageForDomain = '';
146
		$p1->write();
147
		$p1->doPublish();
148
		
149
		$current_theme = Config::inst()->get('SSViewer', 'theme_enabled') ? Config::inst()->get('SSViewer', 'theme') : null;
150
		$this->assertEquals($current_theme, $default_theme, 'After a standard publication, the theme is correct');
151
		
152
		//We can set the static_publishing theme to something completely different:
153
		//Static publishing will use this one instead of the current_custom_theme if it is not false
154
		Config::inst()->update('FilesystemPublisher', 'static_publisher_theme', 'otherTheme');
155
		$current_theme = Config::inst()->get('FilesystemPublisher', 'static_publisher_theme');
156
157
		$this->assertNotEquals($current_theme, $default_theme, 'The static publisher theme overrides the custom theme');
158
	}
159
160
	public function testMenu2LinkingMode() { 
161
		$this->logInWithPermission('ADMIN'); 
162
		
163
		Config::inst()->update('SSViewer', 'theme', null);
164
		
165
		$l1 = new StaticPublisherTestPage(); 
166
		$l1->URLSegment = strtolower(__CLASS__).'-level-1'; 
167
 		$l1->write(); 
168
		$l1->doPublish(); 
169
	
170
		$l2_1 = new StaticPublisherTestPage(); 
171
		$l2_1->URLSegment = strtolower(__CLASS__).'-level-2-1'; 
172
		$l2_1->ParentID = $l1->ID; 
173
		$l2_1->write(); 
174
		
175
		$l2_1->doPublish(); 
176
		$response = Director::test($l2_1->AbsoluteLink());
177
178
		$this->assertEquals(trim($response->getBody()), "current", "current page is level 2-1"); 
179
                
180
		$l2_2 = new StaticPublisherTestPage(); 
181
		$l2_2->URLSegment = strtolower(__CLASS__).'-level-2-2'; 
182
		$l2_2->ParentID = $l1->ID; 
183
		$l2_2->write(); 
184
		$l2_2->doPublish();
185
186
		$response = Director::test($l2_2->AbsoluteLink()); 
187
		$this->assertEquals(trim($response->getBody()), "linkcurrent", "current page is level 2-2"); 
188
	}
189
190 View Code Duplication
	public function testContentTypeHTML() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
		StaticPublisherTestPage::remove_extension('FilesystemPublisher');
192
		StaticPublisherTestPage::add_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/', 'php')");
193
		$l1 = new StaticPublisherTestPage();
194
		$l1->URLSegment = 'mimetype';
195
 		$l1->write();
196
		$l1->doPublish();
197
		$response = Director::test('mimetype');
198
		$this->assertEquals($response->getHeader('Content-Type'), 'text/html; charset=utf-8', 'Content-Type should be text/html; charset=utf-8');
199
		StaticPublisherTestPage::remove_extension('FilesystemPublisher');
200
		StaticPublisherTestPage::add_extension('FilesystemPublisher');
201
	}
202
203 View Code Duplication
	public function testContentTypeJSON() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
		StaticPublisherTestPage::remove_extension('FilesystemPublisher');
205
		StaticPublisherTestPage::add_extension("FilesystemPublisher('assets/FilesystemPublisherTest-static-folder/', 'php')");
206
		$l1 = new StaticPublisherTestPage();
207
		$l1->URLSegment = 'mimetype';
208
 		$l1->write();
209
		$l1->doPublish();
210
		$response = Director::test('mimetype/json');
211
		$this->assertEquals($response->getHeader('Content-Type'), 'application/json', 'Content-Type should be application/json');
212
		StaticPublisherTestPage::remove_extension('FilesystemPublisher');
213
		StaticPublisherTestPage::add_extension('FilesystemPublisher');
214
	}
215
}
216
217
/**
218
 * @package staticpublisher
219
 */
220
class StaticPublisherTestPage extends Page implements TestOnly {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
221
222
	private static $allowed_children = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_children is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
223
		'StaticPublisherTestPage'
224
	);
225
226
227
	public function canPublish($member = null) { 
228
		return true; 
229
	}
230
231
	public function getTemplate() {
232
		return STATIC_MODULE_DIR . '/tests/templates/StaticPublisherTestPage.ss';
233
	}
234
} 
235
236
/**
237
 * @package staticpublisher
238
 */
239
class StaticPublisherTestPage_Controller extends Page_Controller implements TestOnly {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
240
241
	/**
242
	 *
243
	 * @var array
244
	 */
245
	private static $allowed_actions = array('json');
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
246
	
247
	public function json(SS_HTTPRequest $request) {
248
		$response = new SS_HTTPResponse('{"firstName": "John"}');
249
		$response->addHeader('Content-Type', 'application/json');
250
		return $response;
251
252
	}
253
254
}
255