forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-multisite.php
266 lines (233 loc) · 9.04 KB
/
test-timber-multisite.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
class TestTimberMultisite extends Timber_UnitTestCase
{
public function set_up()
{
self::clear();
parent::set_up();
}
public function testGetSubDomainSites()
{
if (!is_multisite()) {
$this->markTestSkipped("You can't get sites except on Multisite");
return;
}
$bids[] = self::createSubDomainSite('foo.example.org', 'My Foo');
$bids[] = self::createSubDomainSite('quack.example.org', "Ducks R Us");
$sites = Timber::get_sites();
$this->assertEquals('http://foo.example.org', $sites[1]->url);
$this->assertEquals("Ducks R Us", $sites[2]->name);
$this->assertEquals('http://quack.example.org', $sites[2]->link());
}
public function testGetSubDirectorySites()
{
if (!is_multisite()) {
$this->markTestSkipped("You can't get sites except on Multisite");
return;
}
$bids[] = self::createSubDirectorySite('/bar/', 'My Bar');
$bids[] = self::createSubDirectorySite('/bark/', "Barks R Us");
$sites = Timber::get_sites();
$this->assertEquals('http://example.org/bark', $sites[2]->url);
$this->assertEquals('http://example.org/bar', $sites[1]->url);
$this->assertEquals("example.org", $sites[2]->domain);
}
public function testPostGettingAcrossSites()
{
if (!is_multisite()) {
$this->markTestSkipped("You can't get sites except on Multisite");
return;
}
$site_ids[] = self::createSubDomainSite('foo.example.org', 'My Foo');
$site_ids[] = self::createSubDomainSite('quack.example.org', "Ducks R Us");
$site_ids[] = self::createSubDomainSite('duck.example.org', "More Ducks R Us");
$post_titles = ["I don't like zebras", "Zebra and a half", "Have a zebra of a time"];
//$others = $this->factory->post->create_many(8);
foreach ($site_ids as $site_id) {
switch_to_blog($site_id);
$this->factory->post->create([
'post_title' => array_pop($post_titles),
]);
}
$timber_posts = [];
$wp_posts = [];
$sites = Timber::get_sites();
foreach ($sites as $site) {
switch_to_blog($site->blog_id);
//error_log(print_r($site, true));
// fetch all the posts
$timber_query = Timber::get_posts([
'post_type' => 'post',
]);
foreach ($timber_query as $post) {
$timber_posts[] = $post;
}
$wp_query = get_posts([
'post_type' => 'post',
]);
foreach ($wp_query as $post) {
$wp_posts[] = $post;
}
restore_current_blog();
// display all posts
}
$this->assertSame(6, count($timber_posts));
$this->assertSame(6, count($wp_posts));
// ensure tha the current site's post count is distinct from our test condition
$current_site_all_posts = get_posts([
'post_type' => 'post',
]);
$this->assertSame(2, count($current_site_all_posts));
}
/**
* @ticket #2269
*/
public function testPostGettingAcrossSitesNoArgs()
{
if (!is_multisite()) {
$this->markTestSkipped("You can't get sites except on Multisite");
return;
}
$site_ids[] = self::createSubDomainSite('foo.example.org', 'My Foo');
$site_ids[] = self::createSubDomainSite('quack.example.org', "Ducks R Us");
$site_ids[] = self::createSubDomainSite('duck.example.org', "More Ducks R Us");
$post_titles = ["I don't like zebras", "Zebra and a half", "Have a zebra of a time"];
foreach ($site_ids as $site_id) {
switch_to_blog($site_id);
$this->factory->post->create([
'post_title' => 'Zebras are good on site ID = ' . $site_id,
]);
}
$this->go_to('/');
$timber_posts = [];
$wp_posts = [];
$sites = Timber::get_sites();
foreach ($sites as $site) {
switch_to_blog($site->blog_id);
// fetch all the posts
$timber_query = Timber::get_posts();
foreach ($timber_query as $post) {
$timber_posts[] = $post;
}
$wp_query = get_posts();
foreach ($wp_query as $post) {
$wp_posts[] = $post;
}
restore_current_blog();
// display all posts
}
// testing that in multisite we get back posts in a loop
$this->assertGreaterThan(0, count($timber_posts));
$this->assertGreaterThan(0, count($wp_posts));
$this->markTestIncomplete(
"WordPress's get_posts() and Timber::get_posts() behave differently here. This could be resolved in the future with investigations on defaults with no arguments and they should be handled"
);
}
public function testPostSearchAcrossSites()
{
if (!is_multisite()) {
$this->markTestSkipped("You can't get sites except on Multisite");
return;
}
$site_ids[] = self::createSubDomainSite('foo.example.org', 'My Foo');
$site_ids[] = self::createSubDomainSite('quack.example.org', "Ducks R Us");
$site_ids[] = self::createSubDomainSite('duck.example.org', "More Ducks R Us");
$post_titles = ["I don't like zebras", "Zebra and a half", "Have a zebra of a time"];
$others = $this->factory->post->create_many(8);
foreach ($site_ids as $site_id) {
switch_to_blog($site_id);
$this->factory->post->create([
'post_title' => array_pop($post_titles),
]);
}
$timber_posts = [];
$wp_posts = [];
$sites = Timber::get_sites();
foreach ($sites as $site) {
switch_to_blog($site->blog_id);
// fetch all the posts
$timber_query = Timber::get_posts([
's' => 'zebra',
]);
foreach ($timber_query as $post) {
$timber_posts[] = $post;
}
$wp_query = get_posts([
's' => 'zebra',
]);
foreach ($wp_query as $post) {
$wp_posts[] = $post;
}
restore_current_blog();
// display all posts
}
$this->assertSame(3, count($timber_posts));
$this->assertSame(3, count($wp_posts));
// ensure tha the current site's post count is distinct from our test condition
$current_site_all_posts = get_posts();
$this->assertSame(5, count($current_site_all_posts));
}
/**
* Tests whether images accessed with switch_to_blog() get the correct url.
*
* @ticket https://github.com/timber/timber/issues/1312
*/
public function test_switch_to_blog_with_timber_images()
{
if (!is_multisite()) {
$this->markTestSkipped("You can't get sites except on Multisite");
return;
}
// Load image and cache for Timber\Image::wp_upload_dir() in site 1.
$image_1 = Timber::get_image(TestTimberImage::get_attachment());
// Create site 2 and switch to it.
self::createSubDirectorySite('/site-2/', 'Site 2');
// Create and load image in site 2.
$site_2_upload_dir = wp_upload_dir();
$image_2 = Timber::get_image(TestTimberImage::get_attachment());
$image_2_src = (string) $image_2->src();
restore_current_blog();
$this->assertStringStartsWith($site_2_upload_dir['baseurl'], $image_2_src);
// test resizing
$template = '{{ image|resize(300, 300) }}?template=true';
$img_resized_src = Timber::compile_string($template, [
'image' => $image_2_src,
]);
$this->assertStringStartsWith($site_2_upload_dir['baseurl'], $img_resized_src);
}
public function testTimberSiteWPObject()
{
$this->skipWithoutMultisite();
$ts = new Timber\Site();
$this->assertInstanceOf('WP_Site', $ts->wp_object());
}
public static function createSubDomainSite($domain = 'test.example.org', $title = 'Multisite Test')
{
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$blog_id = wpmu_create_blog($domain, '/', $title, 1);
switch_to_blog($blog_id);
return $blog_id;
}
public static function createSubDirectorySite($dir = '/mysite/', $title = 'Multisite Subdir Test')
{
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$blog_id = wpmu_create_blog('example.org', $dir, $title, 1);
switch_to_blog($blog_id);
return $blog_id;
}
public static function clear()
{
if (!is_multisite()) {
return;
}
global $wpdb;
$query = "DELETE FROM $wpdb->blogs WHERE blog_id > 1";
$wpdb->query($query);
$blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs ORDER BY blog_id ASC");
}
public function tear_down()
{
self::clear();
parent::tear_down();
}
}