Skip to content

Commit 75901c4

Browse files
Add file required for unit tests
1 parent 7ea7419 commit 75901c4

1 file changed

Lines changed: 246 additions & 0 deletions

File tree

test/helpers/wordpress.php

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
<?php
2+
3+
define( 'ABSPATH', dirname( dirname( __FILE__ ) ) . '/' );
4+
define( 'WPINC', 'wp-includes-for-tests' );
5+
6+
use org\bovigo\vfs\vfsStream;
7+
use org\bovigo\vfs\content\LargeFileContent;
8+
9+
class WordPressOptions {
10+
private $values;
11+
12+
public function __construct() {
13+
$this->values = array(
14+
'thumbnail_size_w' => 150,
15+
'thumbnail_size_h' => 150,
16+
'medium_size_w' => 300,
17+
'medium_size_h' => 300,
18+
'medium_large_size_w' => 768,
19+
'medium_large_size_h' => 0,
20+
'large_size_w' => 1024,
21+
'large_size_h' => 1024,
22+
);
23+
}
24+
25+
public function set( $key, $value ) {
26+
if ( preg_match( '#^(.+)\[(.+)\]$#', $key, $match ) ) {
27+
if ( ! isset( $this->values[ $match[1] ] ) ) {
28+
$this->values[ $match[1] ] = array();
29+
}
30+
$this->values[ $match[1] ][ $match[2] ] = $value;
31+
} else {
32+
$this->values[ $key ] = $value;
33+
}
34+
}
35+
36+
public function get( $key, $default = null ) {
37+
return isset( $this->values[ $key ] ) ? $this->values[ $key ] : $default;
38+
}
39+
}
40+
41+
class WordPressStubs {
42+
const UPLOAD_DIR = 'wp-content/uploads';
43+
44+
private $vfs;
45+
private $initFunctions;
46+
private $admin_initFunctions;
47+
private $options;
48+
private $metadata;
49+
private $calls;
50+
private $stubs;
51+
52+
public function __construct( $vfs ) {
53+
$GLOBALS['wp'] = $this;
54+
$this->vfs = $vfs;
55+
$this->addMethod( 'add_action' );
56+
$this->addMethod( 'do_action' );
57+
$this->addMethod( 'add_filter' );
58+
$this->addMethod( 'register_setting' );
59+
$this->addMethod( 'add_settings_section' );
60+
$this->addMethod( 'add_settings_field' );
61+
$this->addMethod( 'get_option' );
62+
$this->addMethod( 'get_site_option' );
63+
$this->addMethod( 'update_site_option' );
64+
$this->addMethod( 'get_post_meta' );
65+
$this->addMethod( 'update_post_meta' );
66+
$this->addMethod( 'get_intermediate_image_sizes' );
67+
$this->addMethod( 'add_image_size' );
68+
$this->addMethod( 'translate' );
69+
$this->addMethod( 'load_plugin_textdomain' );
70+
$this->addMethod( 'get_post_mime_type' );
71+
$this->addMethod( 'get_plugin_data' );
72+
$this->addMethod( 'wp_upload_dir' );
73+
$this->addMethod( 'plugin_basename' );
74+
$this->addMethod( 'is_multisite' );
75+
$this->addMethod( 'current_user_can' );
76+
$this->addMethod( 'wp_get_attachment_metadata' );
77+
$this->addMethod( 'is_admin' );
78+
$this->defaults();
79+
$this->create_filesystem();
80+
}
81+
82+
public function create_filesystem() {
83+
vfsStream::newDirectory( self::UPLOAD_DIR )
84+
->at( $this->vfs );
85+
}
86+
87+
public function defaults() {
88+
$this->initFunctions = array();
89+
$this->admin_initFunctions = array();
90+
$this->options = new WordPressOptions();
91+
$this->metadata = array();
92+
$GLOBALS['_wp_additional_image_sizes'] = array();
93+
}
94+
95+
public function call( $method, $args ) {
96+
$this->calls[ $method ][] = $args;
97+
if ( 'add_action' === $method ) {
98+
if ( 'init' === $args[0] ) {
99+
$this->initFunctions[] = $args[1];
100+
} elseif ( 'admin_init' === $args[0] ) {
101+
$this->admin_initFunctions[] = $args[1];
102+
}
103+
}
104+
if ( 'translate' === $method ) {
105+
return $args[0];
106+
} elseif ( 'get_option' === $method ) {
107+
return call_user_func_array( array( $this->options, 'get' ), $args );
108+
} elseif ( 'get_post_meta' === $method ) {
109+
return call_user_func_array( array( $this, 'getMetadata' ), $args );
110+
} elseif ( 'add_image_size' === $method ) {
111+
return call_user_func_array( array( $this, 'addImageSize' ), $args );
112+
} elseif ( 'update_post_meta' === $method ) {
113+
return call_user_func_array( array( $this, 'updateMetadata' ), $args );
114+
} elseif ( 'get_intermediate_image_sizes' === $method ) {
115+
return array_merge( array( 'thumbnail', 'medium', 'medium_large', 'large' ), array_keys( $GLOBALS['_wp_additional_image_sizes'] ) );
116+
} elseif ( 'get_plugin_data' === $method ) {
117+
return array( 'Version' => '1.7.2' );
118+
} elseif ( 'wp_upload_dir' === $method ) {
119+
return array( 'basedir' => $this->vfs->url() . '/' . self::UPLOAD_DIR, 'baseurl' => '/' . self::UPLOAD_DIR );
120+
} elseif ( 'is_admin' === $method ) {
121+
return true;
122+
} elseif ( $this->stubs[ $method ] ) {
123+
return call_user_func_array( $this->stubs[ $method ], $args );
124+
}
125+
}
126+
127+
public function addMethod( $method ) {
128+
$this->calls[ $method ] = array();
129+
$this->stubs[ $method ] = array();
130+
if ( ! function_exists( $method ) ) {
131+
eval( "function $method() { return \$GLOBALS['wp']->call('$method', func_get_args()); }" );
132+
}
133+
}
134+
135+
public function addOption( $key, $value ) {
136+
$this->options->set( $key, $value );
137+
}
138+
139+
public function addImageSize( $size, $values ) {
140+
$GLOBALS['_wp_additional_image_sizes'][ $size ] = $values;
141+
}
142+
143+
public function getMetadata( $id, $key, $single = false ) {
144+
$values = isset( $this->metadata[ $id ] ) ? $this->metadata[ $id ] : array();
145+
$value = isset( $values[ $key ] ) ? $values[ $key ] : '';
146+
return $single ? $value : array( $value );
147+
}
148+
149+
public function updateMetadata( $id, $key, $values ) {
150+
$this->metadata[ $id ][ $key ] = $values;
151+
}
152+
153+
public function setTinyMetadata( $id, $values ) {
154+
$this->metadata[ $id ] = array( Tiny_Config::META_KEY => $values );
155+
}
156+
157+
public function getCalls( $method ) {
158+
return $this->calls[ $method ];
159+
}
160+
161+
public function init() {
162+
foreach ( $this->initFunctions as $func ) {
163+
call_user_func( $func );
164+
}
165+
}
166+
167+
public function admin_init() {
168+
foreach ( $this->admin_initFunctions as $func ) {
169+
call_user_func( $func );
170+
}
171+
}
172+
173+
public function stub( $method, $func ) {
174+
$this->stubs[ $method ] = $func;
175+
}
176+
177+
public function createImage( $file_size, $path, $name ) {
178+
if ( ! $this->vfs->hasChild( self::UPLOAD_DIR . "/$path" ) ) {
179+
vfsStream::newDirectory( self::UPLOAD_DIR . "/$path" )->at( $this->vfs );
180+
}
181+
$dir = $this->vfs->getChild( self::UPLOAD_DIR . "/$path" );
182+
183+
vfsStream::newFile( $name )
184+
->withContent( new LargeFileContent( $file_size ) )
185+
->at( $dir );
186+
}
187+
188+
public function createImages( $sizes = null, $original_size = 12345, $path = '14/01', $name = 'test' ) {
189+
vfsStream::newDirectory( self::UPLOAD_DIR . "/$path" )->at( $this->vfs );
190+
$dir = $this->vfs->getChild( self::UPLOAD_DIR . '/' . $path );
191+
192+
vfsStream::newFile( "$name.png" )
193+
->withContent( new LargeFileContent( $original_size ) )
194+
->at( $dir );
195+
196+
if ( is_null( $sizes ) ) {
197+
$sizes = array( 'thumbnail' => 100, 'medium' => 1000 , 'large' => 10000, 'post-thumbnail' => 1234 );
198+
}
199+
200+
foreach ( $sizes as $key => $size ) {
201+
vfsStream::newFile( "$name-$key.png" )
202+
->withContent( new LargeFileContent( $size ) )
203+
->at( $dir );
204+
}
205+
}
206+
207+
public function createImagesFromJSON( $virtual_images ) {
208+
foreach ( $virtual_images['images'] as $image ) {
209+
self::createImage( $image['size'], $virtual_images['path'], $image['file'] );
210+
}
211+
}
212+
213+
public function getTestMetadata( $path = '14/01', $name = 'test' ) {
214+
$metadata = array(
215+
'file' => "$path/$name.png",
216+
'width' => 4000,
217+
'height' => 3000,
218+
'sizes' => array(),
219+
);
220+
221+
$regex = '#^' . preg_quote( $name ) . '-([^.]+)[.](png|jpe?g)$#';
222+
$dir = $this->vfs->getChild( self::UPLOAD_DIR . "/$path" );
223+
foreach ( $dir->getChildren() as $child ) {
224+
$file = $child->getName();
225+
if ( preg_match( $regex, $file, $match ) ) {
226+
$metadata['sizes'][ $match[1] ] = array( 'file' => $file );
227+
}
228+
}
229+
230+
return $metadata;
231+
}
232+
}
233+
234+
class WP_HTTP_Proxy {
235+
public function is_enabled() {
236+
return false;
237+
}
238+
}
239+
240+
function __( $text, $domain = 'default' ) {
241+
return translate( $text, $domain );
242+
}
243+
244+
function esc_html__( $text, $domain = 'default' ) {
245+
return translate( $text, $domain );
246+
}

0 commit comments

Comments
 (0)