/
factory.php
344 lines (280 loc) · 9.97 KB
/
factory.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
class WP_UnitTest_Factory {
/**
* @var WP_UnitTest_Factory_For_Post
*/
public $post;
/**
* @var WP_UnitTest_Factory_For_Attachment
*/
public $attachment;
/**
* @var WP_UnitTest_Factory_For_Comment
*/
public $comment;
/**
* @var WP_UnitTest_Factory_For_User
*/
public $user;
/**
* @var WP_UnitTest_Factory_For_Term
*/
public $term;
/**
* @var WP_UnitTest_Factory_For_Term
*/
public $category;
/**
* @var WP_UnitTest_Factory_For_Term
*/
public $tag;
/**
* @var WP_UnitTest_Factory_For_Blog
*/
public $blog;
function __construct() {
$this->post = new WP_UnitTest_Factory_For_Post( $this );
$this->attachment = new WP_UnitTest_Factory_For_Attachment( $this );
$this->comment = new WP_UnitTest_Factory_For_Comment( $this );
$this->user = new WP_UnitTest_Factory_For_User( $this );
$this->term = new WP_UnitTest_Factory_For_Term( $this );
$this->category = new WP_UnitTest_Factory_For_Term( $this, 'category' );
$this->tag = new WP_UnitTest_Factory_For_Term( $this, 'post_tag' );
if ( is_multisite() )
$this->blog = new WP_UnitTest_Factory_For_Blog( $this );
}
}
class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
function __construct( $factory = null ) {
parent::__construct( $factory );
$this->default_generation_definitions = array(
'post_status' => 'publish',
'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
'post_type' => 'post'
);
}
function create_object( $args ) {
return wp_insert_post( $args );
}
function update_object( $post_id, $fields ) {
$fields['ID'] = $post_id;
return wp_update_post( $fields );
}
function get_object_by_id( $post_id ) {
return get_post( $post_id );
}
}
class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
function create_object( $file, $parent = 0, $args = array() ) {
return wp_insert_attachment( $args, $file, $parent );
}
}
class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
function __construct( $factory = null ) {
parent::__construct( $factory );
$this->default_generation_definitions = array(
'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
'user_pass' => 'password',
'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
);
}
function create_object( $args ) {
return wp_insert_user( $args );
}
function update_object( $user_id, $fields ) {
$fields['ID'] = $user_id;
return wp_update_user( $fields );
}
function get_object_by_id( $user_id ) {
return new WP_User( $user_id );
}
}
class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
function __construct( $factory = null ) {
parent::__construct( $factory );
$this->default_generation_definitions = array(
'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
'comment_approved' => 1,
);
}
function create_object( $args ) {
return wp_insert_comment( $this->addslashes_deep( $args ) );
}
function update_object( $comment_id, $fields ) {
$fields['comment_ID'] = $comment_id;
return wp_update_comment( $this->addslashes_deep( $fields ) );
}
function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
$args['comment_post_ID'] = $post_id;
return $this->create_many( $count, $args, $generation_definitions );
}
function get_object_by_id( $comment_id ) {
return get_comment( $comment_id );
}
}
class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
function __construct( $factory = null ) {
global $current_site, $base;
parent::__construct( $factory );
$this->default_generation_definitions = array(
'domain' => $current_site->domain,
'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
'site_id' => $current_site->id,
);
}
function create_object( $args ) {
$meta = isset( $args['meta'] ) ? $args['meta'] : array();
$user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
return wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
}
function update_object( $blog_id, $fields ) {}
function get_object_by_id( $blog_id ) {
return get_blog_details( $blog_id, false );
}
}
class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
private $taxonomy;
const DEFAULT_TAXONOMY = 'post_tag';
function __construct( $factory = null, $taxonomy = null ) {
parent::__construct( $factory );
$this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
$this->default_generation_definitions = array(
'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
'taxonomy' => $this->taxonomy,
'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
);
}
function create_object( $args ) {
$args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
$term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
if ( is_wp_error( $term_id_pair ) )
return $term_id_pair;
return $term_id_pair['term_id'];
}
function update_object( $term, $fields ) {
$fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
if ( is_object( $term ) )
$taxonomy = $term->taxonomy;
$term_id_pair = wp_update_term( $term, $taxonomy, $fields );
return $term_id_pair['term_id'];
}
function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
}
function get_object_by_id( $term_id ) {
return get_term( $term_id, $this->taxonomy );
}
}
abstract class WP_UnitTest_Factory_For_Thing {
var $default_generation_definitions;
var $factory;
/**
* Creates a new factory, which will create objects of a specific Thing
*
* @param object $factory Global factory that can be used to create other objects on the system
* @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
* can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
* {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
*/
function __construct( $factory, $default_generation_definitions = array() ) {
$this->factory = $factory;
$this->default_generation_definitions = $default_generation_definitions;
}
abstract function create_object( $args );
abstract function update_object( $object, $fields );
function create( $args = array(), $generation_definitions = null ) {
if ( is_null( $generation_definitions ) )
$generation_definitions = $this->default_generation_definitions;
$generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
$created = $this->create_object( $generated_args );
if ( !$created || is_wp_error( $created ) )
return $created;
if ( $callbacks ) {
$updated_fields = $this->apply_callbacks( $callbacks, $created );
$save_result = $this->update_object( $created, $updated_fields );
if ( !$save_result || is_wp_error( $save_result ) )
return $save_result;
}
return $created;
}
function create_and_get( $args = array(), $generation_definitions = null ) {
$object_id = $this->create( $args, $generation_definitions );
return $this->get_object_by_id( $object_id );
}
abstract function get_object_by_id( $object_id );
function create_many( $count, $args = array(), $generation_definitions = null ) {
$results = array();
for ( $i = 0; $i < $count; $i++ ) {
$results[] = $this->create( $args, $generation_definitions );
}
return $results;
}
function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
$callbacks = array();
if ( is_null( $generation_definitions ) )
$generation_definitions = $this->default_generation_definitions;
foreach( array_keys( $generation_definitions ) as $field_name ) {
if ( !isset( $args[$field_name] ) ) {
$generator = $generation_definitions[$field_name];
if ( is_scalar( $generator ) )
$args[$field_name] = $generator;
elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
$callbacks[$field_name] = $generator;
} elseif ( is_object( $generator ) )
$args[$field_name] = $generator->next();
else
return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
}
}
return $args;
}
function apply_callbacks( $callbacks, $created ) {
$updated_fields = array();
foreach( $callbacks as $field_name => $generator ) {
$updated_fields[$field_name] = $generator->call( $created );
}
return $updated_fields;
}
function callback( $function ) {
return new WP_UnitTest_Factory_Callback_After_Create( $function );
}
function addslashes_deep($value) {
if ( is_array( $value ) ) {
$value = array_map( array( $this, 'addslashes_deep' ), $value );
} elseif ( is_object( $value ) ) {
$vars = get_object_vars( $value );
foreach ($vars as $key=>$data) {
$value->{$key} = $this->addslashes_deep( $data );
}
} elseif ( is_string( $value ) ) {
$value = addslashes( $value );
}
return $value;
}
}
class WP_UnitTest_Generator_Sequence {
var $next;
var $template_string;
function __construct( $template_string = '%s', $start = 1 ) {
$this->next = $start;
$this->template_string = $template_string;
}
function next() {
$generated = sprintf( $this->template_string , $this->next );
$this->next++;
return $generated;
}
}
class WP_UnitTest_Factory_Callback_After_Create {
var $callback;
function __construct( $callback ) {
$this->callback = $callback;
}
function call( $object ) {
return call_user_func( $this->callback, $object );
}
}