diff --git a/includes/cli-server/router.php b/includes/cli-server/router.php index 2c3209d96..d7e85a3fe 100644 --- a/includes/cli-server/router.php +++ b/includes/cli-server/router.php @@ -9,28 +9,35 @@ } $root = $_SERVER['DOCUMENT_ROOT']; -$path = '/'. ltrim( parse_url( urldecode( $_SERVER['REQUEST_URI'] ),PHP_URL_PATH ), '/' ); +$path = '/' . ltrim(parse_url(urldecode($_SERVER['REQUEST_URI']), PHP_URL_PATH), '/'); define('DB_ENGINE', getenv('DB_ENGINE') ?: 'mysql'); -if ( file_exists( $root.$path ) ) { +// The self-call will slow down the response handling and will not benefit the test environment. +global $wp_filter; +$wp_filter['do_mu_upgrade'][10][] = [ + 'accepted_args' => 0, + 'function' => static function () { + return false; + } +]; - // Enforces trailing slash, keeping links tidy in the admin - if ( is_dir( $root.$path ) && substr_compare($path, '/', -strlen('/')) !== 0 ) { - header( "Location: $path/" ); - exit; - } +if (file_exists($root . $path)) { + // Enforces trailing slash, keeping links tidy in the admin + if (is_dir($root . $path) && substr_compare($path, '/', -strlen('/')) !== 0) { + header("Location: $path/"); + exit; + } - // Runs PHP file if it exists - if ( strpos($path, '.php') !== false ) { - chdir( dirname( $root.$path ) ); - require_once $root.$path; - } else { - return false; - } + // Runs PHP file if it exists + if (strpos($path, '.php') !== false) { + chdir(dirname($root . $path)); + require_once $root . $path; + } else { + return false; + } } else { - - // Otherwise, run `index.php` - chdir( $root ); - require_once 'index.php'; + // Otherwise, run `index.php` + chdir($root); + require_once 'index.php'; } diff --git a/src/Module/WPLoader/Filters.php b/src/Module/WPLoader/Filters.php deleted file mode 100644 index 336bd88a2..000000000 --- a/src/Module/WPLoader/Filters.php +++ /dev/null @@ -1,205 +0,0 @@ -> - */ - protected $toRemove = []; - - /** - * The list of filters to add. - * - * @var array> - */ - protected $toAdd = []; - - /** - * Filters constructor. - * - * @param array> $filters The filters to manage. - */ - public function __construct(array $filters = []) - { - $this->toRemove = !empty($filters['remove']) - ? array_map([$this, 'normalizeFilter'], $filters['remove']) - : []; - $this->toAdd = !empty($filters['add']) - ? array_map([$this, 'normalizeFilter'], $filters['add']) - : []; - } - - /** - * Formats and normalizes a list of filters. - * - * @param array> $filters The list of filters to format. - * - * @return array{remove: mixed[][], add: mixed[][]} The formatted list of filters. - */ - public static function format(array $filters): array - { - $instance = new self($filters); - - return $instance->toArray(); - } - - /** - * Returns the current state in array format. - * - * @return array{remove: mixed[][], add: mixed[][]} A map of the filters to remove and to add.j:w - */ - public function toArray(): array - { - return [ - 'remove' => $this->toRemove, - 'add' => $this->toAdd, - ]; - } - - /** - * Returns the list of filters to remove. - * - * @return FiltersGroup The group of filters to remove. - */ - public function toRemove(): FiltersGroup - { - return new FiltersGroup($this->toRemove, $this->removeWith, $this->addWith); - } - - /** - * Sets the callable that should be used to remove the filters. - * - * @param callable $removeWith The callable that should be used to remove the filters. - */ - public function removeWith(callable $removeWith): void - { - $this->removeWith = $removeWith; - } - - /** - * Sets the callable that should be used to remove the filters. - * - * @param callable $addWith The callable that should be used to add the filters. - */ - public function addWith(callable $addWith): void - { - $this->addWith = $addWith; - } - - /** - * Returns the list of filters to add. - * - * @return FiltersGroup The group of filters to add. - */ - public function toAdd(): FiltersGroup - { - return new FiltersGroup($this->toAdd, $this->removeWith, $this->addWith); - } - - /** - * Normalizes a filter contents. - * - * @param array $filter The current filter state. - * - * @return array The normalized filter. - * - * @throws ModuleException If the filters information is not complete or not coherent. - */ - protected function normalizeFilter(array $filter): array - { - if (count($filter) < 2) { - throw new ModuleException( - __CLASS__, - 'Callback ' . json_encode($filter) . ' does not specify enough data for a filter: ' - . 'required at least tag and callback.' - ); - } - - if (empty($filter[0]) || !is_string($filter[0])) { - throw new ModuleException(__CLASS__, 'Callback ' . json_encode($filter) . ' does not specify a valid tag.'); - } - - if (count($filter) === 2) { - $filter[] = $this->defaultPriority; - } - - if (count($filter) === 3) { - $filter[] = $this->defaultAcceptedArguments; - } - - if (count($filter) > 4) { - throw new ModuleException( - __CLASS__, - 'Callback ' . json_encode($filter) . ' contains too many arguments; ' - .'only tag, callback, priority and accepted arguments are supported' - ); - } - - - $callbackFunc = $filter[1]; - - if (empty($callbackFunc) || !(is_string($callbackFunc) || is_array($callbackFunc))) { - throw new ModuleException( - __CLASS__, - 'Callback for ' . json_encode($filter) . ' is empty or the wrong type: ' - .'it should be a string (a function name) or an array of two strings (class name and a static method).' - ); - } - - if (is_array($callbackFunc)) { - if (count($callbackFunc) !== 2 - || count(array_filter($callbackFunc, 'is_string')) !== 2 - ) { - throw new ModuleException( - __CLASS__, - 'Callback for ' . json_encode($filter) . ' is weird: ' - .'it should be a string (function name) or an array of two strings (class name and static method).' - ); - } - } - - return $filter; - } -} diff --git a/src/Module/WPLoader/FiltersGroup.php b/src/Module/WPLoader/FiltersGroup.php deleted file mode 100644 index 3e7628a5f..000000000 --- a/src/Module/WPLoader/FiltersGroup.php +++ /dev/null @@ -1,76 +0,0 @@ -> - */ - protected $filters = []; - /** - * The callback that will be used to remove filters. - * - * @var callable - */ - protected $removeCallback; - - /** - * The callback that will be used to add filters. - * - * @var callable - */ - protected $addCallback; - - /** - * FiltersGroup constructor. - * - * @param array> $filters The list of filters to manage. - * @param callable|null $removeWith The callable that should be used to remove the filters or `null` to use - * the default one. - * @param callable|null $addWith The callable that should be used to add the filters, or `null` to use the - */ - public function __construct( - array $filters = [], - callable $removeWith = null, - callable $addWith = null - ) { - /** - * An array detailing each filter callback, priority and arguments. - */ - $this->filters = $filters; - $this->removeCallback = $removeWith ?? 'remove_filter'; - $this->addCallback = $addWith ?? 'add_filter'; - } - - /** - * Removes the filters of the group. - */ - public function remove(): void - { - foreach ($this->filters as $filter) { - $filterWithoutAcceptedArguments = array_slice($filter, 0, 3); - call_user_func_array($this->removeCallback, $filterWithoutAcceptedArguments); - } - } - - /** - * Adds the filters of the group. - */ - public function add(): void - { - foreach ($this->filters as $filter) { - call_user_func_array($this->addCallback, $filter); - } - } -} diff --git a/tests/unit/lucatume/WPBrowser/Events/Module/WPLoader/FiltersTest.php b/tests/unit/lucatume/WPBrowser/Events/Module/WPLoader/FiltersTest.php deleted file mode 100644 index 06fc0170d..000000000 --- a/tests/unit/lucatume/WPBrowser/Events/Module/WPLoader/FiltersTest.php +++ /dev/null @@ -1,200 +0,0 @@ - [ - [ 'some-filter', 'some_callback', 23 ], - [ 'some-other-filter', 'some_callback' ], - [ 'some-filter', 'some_callback' ], - ] - ]); - - $removed = []; - $sut->removeWith(function ($tag, $callback, $priority) use (&$removed) { - $removed[] = [ $tag, $callback, $priority ]; - - return true; - }); - - $sut->toRemove()->remove(); - - $this->assertEquals([ - [ 'some-filter', 'some_callback', 23 ], - [ 'some-other-filter', 'some_callback', 10 ], - [ 'some-filter', 'some_callback', 10 ], - ], $removed); - } - - /** - * It should allow readding removed filters - * - * @test - */ - public function should_allow_readding_removed_filters() - { - $sut = new Filters([ - 'remove' => [ - [ 'some-filter', 'some_callback', 23 ], - [ 'some-other-filter', 'some_callback', 10, 2 ], - [ 'some-filter', 'some_callback' ], - ] - ]); - - $added = []; - $sut->removeWith(function ($tag, $callback, $priority) { - }); - $sut->addWith(function ($tag, $callback, $priority, $acceptedArgs) use (&$added) { - $added[] = [ $tag, $callback, $priority, $acceptedArgs ]; - }); - - $sut->toRemove()->add(); - - $this->assertEquals([ - [ 'some-filter', 'some_callback', 23, 1 ], - [ 'some-other-filter', 'some_callback', 10, 2 ], - [ 'some-filter', 'some_callback', 10, 1 ], - ], $added); - } - - /** - * It should allow setting filters to add - * - * @test - */ - public function should_allow_setting_filters_to_add() - { - $sut = new Filters([ - 'add' => [ - [ 'some-filter', 'some_callback', 23 ], - [ 'some-other-filter', 'some_callback' ], - [ 'some-filter', 'some_callback', 12, 3 ], - ] - ]); - - $added = []; - $sut->addWith(function ($tag, $callback, $priority, $acceptedArgs) use (&$added) { - $added[] = [ $tag, $callback, $priority, $acceptedArgs ]; - - return true; - }); - - $sut->toAdd()->add(); - - $this->assertEquals([ - [ 'some-filter', 'some_callback', 23, 1 ], - [ 'some-other-filter', 'some_callback', 10, 1 ], - [ 'some-filter', 'some_callback', 12, 3 ], - ], $added); - } - - /** - * It should allow removing added filters - * - * @test - */ - public function should_allow_removing_added_filters() - { - $sut = new Filters([ - 'add' => [ - [ 'some-filter', 'some_callback', 23 ], - [ 'some-other-filter', 'some_callback' ], - [ 'some-filter', 'some_callback', 12, 3 ], - ] - ]); - - $removed = []; - $sut->addWith(function () { - }); - $sut->removeWith(function ($tag, $callback, $priority) use (&$removed) { - $removed[] = [ $tag, $callback, $priority ]; - }); - - $sut->toAdd()->remove(); - - $this->assertEquals([ - [ 'some-filter', 'some_callback', 23 ], - [ 'some-other-filter', 'some_callback', 10 ], - [ 'some-filter', 'some_callback', 12 ], - ], $removed); - } - - /** - * It should allow formatting the filters - * - * @test - */ - public function should_allow_formatting_the_filters() - { - $formatted = Filters::format([ - 'foo' => 'bar', - 'add' => [ - [ 'one', 'foo', 11 ], - [ 'one', 'bar' ], - [ 'two', 'foo', 23, 2 ], - ], - 'remove' => [ - [ 'one', 'foo', 11 ], - [ 'one', 'bar' ], - [ 'two', 'foo', 23, 2 ], - ] - ]); - - $this->assertEquals([ - 'remove' => [ - [ 'one', 'foo', 11, 1 ], - [ 'one', 'bar', 10, 1 ], - [ 'two', 'foo', 23, 2 ], - ], - 'add' => [ - [ 'one', 'foo', 11, 1 ], - [ 'one', 'bar', 10, 1 ], - [ 'two', 'foo', 23, 2 ], - ] - ], $formatted); - } - - public function badFilters(): array - { - return [ - [ [ 'foo', new stdClass() ] ], - [ [ new stdClass(), 'foo' ] ], - [ [ '', new stdClass() ] ], - [ [ '', '' ] ], - [ [ '' ] ], - [ [] ], - [ [ 'foo', [ 'bar' ] ] ], - [ [ 'foo', [ 'bar', new stdClass() ] ] ], - [ [ 'foo', [ new stdClass(), 'bar' ] ] ], - [ [ 'foo', 'bar', 10, 12, 23 ] ], - ]; - } - - /** - * It should throw if filters information is not correct - * - * @test - * - * @dataProvider badFilters - */ - public function should_throw_if_filters_information_is_not_correct(array $filters) - { - $this->expectException(ModuleException::class); - new Filters([ - 'remove' => [ $filters ] - ]); - } -}