Skip to content

Commit c35602a

Browse files
committed
Доработки.
1 parent a96b4b3 commit c35602a

File tree

4 files changed

+583
-1
lines changed

4 files changed

+583
-1
lines changed

src/Base/WordpressableTestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Prokl\TestingTools\Base\BaseTestCase;
7+
use Prokl\WordpressCi\Traits\WordpressTrait;
78
use Prokl\WordpressCi\Bootstrap;
89
use Prokl\WordpressCi\ClassUtils;
910
use Prokl\WordpressCi\Database;
@@ -24,6 +25,8 @@
2425
*/
2526
class WordpressableTestCase extends BaseTestCase
2627
{
28+
use WordpressTrait;
29+
2730
/**
2831
* @var array $hooks_saved Бэкап хуков.
2932
*/

src/Bootstrap.php

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,95 @@ public static function migrate() : void
4040
public static function bootstrap() : void
4141
{
4242
$_SERVER['DOCUMENT_ROOT'] = realpath(__DIR__ . '/../files/');
43+
44+
$GLOBALS['_wp_die_disabled'] = true;
45+
46+
// Prevent updating translations asynchronously.
47+
self::testsAddFilter('async_update_translation', '__return_false');
48+
// Disable background updates.
49+
self::testsAddFilter('automatic_updater_disabled', '__return_true');
50+
51+
// Preset WordPress options defined in bootstrap file.
52+
// Used to activate themes, plugins, as well as other settings.
53+
if (isset($GLOBALS['wp_tests_options'])) {
54+
function wp_tests_options($value)
55+
{
56+
$key = substr(current_filter(), strlen('pre_option_'));
57+
58+
return $GLOBALS['wp_tests_options'][$key];
59+
}
60+
61+
foreach (array_keys($GLOBALS['wp_tests_options']) as $key) {
62+
self::testsAddFilter('pre_option_'.$key, 'wp_tests_options');
63+
}
64+
}
65+
4366
require_once __DIR__ . '/../files/wp-blog-header.php';
4467
}
45-
}
68+
69+
/**
70+
* Adds hooks before loading WP.
71+
*
72+
* @see add_filter()
73+
*
74+
* @param string $tag The name of the filter to hook the $function_to_add callback to.
75+
* @param callable $function_to_add The callback to be run when the filter is applied.
76+
* @param integer $priority Optional. Used to specify the order in which the functions
77+
* associated with a particular action are executed.
78+
* Lower numbers correspond with earlier execution,
79+
* and functions with the same priority are executed.
80+
* in the order in which they were added to the action. Default 10.
81+
* @param integer $accepted_args Optional. The number of arguments the function accepts. Default 1.
82+
* @return true
83+
*/
84+
public static function testsAddFilter(string $tag, $function_to_add, int $priority = 10, int $accepted_args = 1) : bool
85+
{
86+
global $wp_filter;
87+
88+
if (function_exists('add_filter')) {
89+
add_filter($tag, $function_to_add, $priority, $accepted_args);
90+
} else {
91+
$idx = self::testFilterBuildUniqueId($tag, $function_to_add, $priority);
92+
93+
$wp_filter[$tag][$priority][$idx] = [
94+
'function' => $function_to_add,
95+
'accepted_args' => $accepted_args,
96+
];
97+
}
98+
99+
return true;
100+
}
101+
102+
/**
103+
* Generates a unique function ID based on the given arguments.
104+
*
105+
* @see _wp_filter_build_unique_id()
106+
*
107+
* @param string $tag Unused. The name of the filter to build ID for.
108+
* @param callable $function The function to generate ID for.
109+
* @param integer $priority Unused. The order in which the functions
110+
* associated with a particular action are executed.
111+
* @return string Unique function ID for usage as array key.
112+
*/
113+
private static function testFilterBuildUniqueId(string $tag, $function, $priority)
114+
{
115+
if (is_string($function)) {
116+
return $function;
117+
}
118+
119+
if (is_object($function)) {
120+
// Closures are currently implemented as objects.
121+
$function = array($function, '');
122+
} else {
123+
$function = (array)$function;
124+
}
125+
126+
if (is_object($function[0])) {
127+
// Object class calling.
128+
return spl_object_hash($function[0]).$function[1];
129+
} elseif (is_string($function[0])) {
130+
// Static calling.
131+
return $function[0].'::'.$function[1];
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)