Skip to content
Permalink
Browse files

factory for schema and wp_query arg whitelist modifications

  • Loading branch information...
Shelob9 committed May 28, 2018
1 parent 4efcf40 commit 3268a62f5efc3a8689d9aeafb7057b31aee3f068
@@ -0,0 +1,47 @@
<?php
namespace CalderaLearn\RestSearch\Tests\Mock;
use CalderaLearn\RestSearch\ModifySchemaContract;
class SchemaModifierImplementation implements ModifySchemaContract
{
/**
* @var bool
*/
protected $should;
/**
* @param bool $should
*/
public function setShouldFilter(bool $should)
{
$this->should = $should;
}
/** @inheritdoc */
public function shouldFilter(string $postTypeSlug): bool
{
return (bool)$this->should;
}
/** @inheritdoc */
public function getAdditionalSchemaArguments(): array
{
return [
'post_type ' => [
'default' => 'post',
'description' => __('Post type(s) for search query'),
'type' => 'array',
'items' =>
[
'enum' => [
'posts',
'pages'
],
'type' => 'string',
],
]
];
}
}
@@ -0,0 +1,52 @@
<?php
namespace CalderaLearn\RestSearch\Tests\Unit;
use CalderaLearn\RestSearch\Features\Factory;
class FeatureFactoryTest extends TestCase
{
/**
* Test setting up system using factory
*
* @covers \CalderaLearn\RestSearch\Features\Factory::search()
*/
public function testArgs()
{
$schema = [
'default' => 'post',
'description' => __('Post type(s) for search query'),
'type' => 'array',
//Limit to public post types and allow query by rest base
'items' =>
[
'enum' => [
'posts',
'pages'
],
'type' => 'string',
],
];
$args = [
'post_type' => [
'schema' => $schema,
'default' => 'post',
]
];
$search = Factory::search($args, [ 'post' ]);
$this->assertSame(
[
'post_type' => $schema
],
$search->getSchemaModifier()->getAdditionalSchemaArguments()
);
$this->assertSame(
[ 'post_type' => 'post' ],
$search->getArgsModifier()->getAdditionalQueryArguments()
);
}
}
@@ -0,0 +1,36 @@
<?php
namespace CalderaLearn\RestSearch\Tests\Unit;
use CalderaLearn\RestSearch\ModifyQueryArgsContract;
class QueryArgModifierImplementation implements ModifyQueryArgsContract
{
/**
* @var bool
*/
protected $should;
/**
* @param bool $should
*/
public function setShouldFilter(bool $should)
{
$this->should = $should;
}
/** @inheritdoc */
public function shouldFilter(string $postTypeSlug): bool
{
return (bool) $this->should;
}
/**
* @return array
*/
public function getAdditionalQueryArguments(): array
{
return [ 'post_type' => 'post' ];
}
}
@@ -0,0 +1,84 @@
<?php
namespace CalderaLearn\RestSearch\Tests\Unit;
use CalderaLearn\RestSearch\Features\Search;
use CalderaLearn\RestSearch\ModifySchema;
use CalderaLearn\RestSearch\ModifySchemaContract;
use CalderaLearn\RestSearch\Tests\Mock\SchemaModifierImplementation;
class SearchFeatureGeneratorTest extends TestCase
{
/**
*
* @covers \CalderaLearn\RestSearch\Features\Search::__construct()
* @covers \CalderaLearn\RestSearch\Features\Search::getSchemaModifier()
* @covers \CalderaLearn\RestSearch\Features\Search::getArgsModifier()
*/
public function testSetModifiers()
{
$schemaModifer = new SchemaModifierImplementation();
$queryArgModifier = new QueryArgModifierImplementation();
$search = new Search($queryArgModifier, $schemaModifer);
$this->assertSame($schemaModifer, $search->getSchemaModifier());
$this->assertSame($queryArgModifier, $search->getArgsModifier());
}
/**
* Test that schema is filtered properly
*
* @covers \CalderaLearn\RestSearch\Features\Search::filterSchema()
* @covers \CalderaLearn\RestSearch\Features\Search::getSchemaModifier()
*/
public function testFilterSchema()
{
$schemaModifer = new SchemaModifierImplementation();
$queryArgModifier = new QueryArgModifierImplementation();
$search = new Search($queryArgModifier, $schemaModifer);
$schemaModifer->setShouldFilter(true);
$wpPostTypeMock = \Mockery::mock('WP_Post_Type');
$wpPostTypeMock->name = 'post';
$this->assertTrue($schemaModifer->shouldFilter($wpPostTypeMock));
$this->assertTrue($search->getSchemaModifier()->shouldFilter($wpPostTypeMock));
$otherArgs = [
'page' => [
'default' => 1,
'type' => 'integer'
]
];
$expectArgs = array_merge($otherArgs, $schemaModifer->getAdditionalSchemaArguments());
$args = $search->filterSchema($otherArgs, $wpPostTypeMock);
$this->assertEquals($expectArgs, $args);
}
/**
* Test that filter args are filtered correctly
*
* @since 1.7.0
*/
public function testFilterQueryArgs()
{
$schemaModifer = new SchemaModifierImplementation();
$queryArgModifier = new QueryArgModifierImplementation();
$search = new Search($queryArgModifier, $schemaModifer);
$queryArgModifier->setShouldFilter(true);
$wpPostTypeMock = \Mockery::mock('WP_Post_Type');
$wpPostTypeMock->name = 'post';
$this->assertTrue($queryArgModifier->shouldFilter($wpPostTypeMock));
$this->assertTrue($search->getArgsModifier()->shouldFilter($wpPostTypeMock));
$otherArgs = [
'page' => [
'default' => 1,
'type' => 'integer'
]
];
$expectArgs = array_merge($otherArgs, $queryArgModifier->getAdditionalQueryArguments());
$wpRestRequest = \Mockery::mock('WP_REST_Request');
$args = $search->filterQueryArgs($otherArgs, $wpRestRequest);
$this->assertEquals($expectArgs, $args);
}
}
@@ -0,0 +1,23 @@
<?php
namespace CalderaLearn\RestSearch;
/**
* Interface DoesFilterContract
*
* Interfaces for classes that add filters.
*/
interface DoesFilterContract
{
/**
* Add the filter
*/
public function addHook();
/**
* Remove the filter
*/
public function removeHook();
}
@@ -0,0 +1,93 @@
<?php
namespace CalderaLearn\RestSearch\Features;
use CalderaLearn\RestSearch\ContentGetter\ContentGetterContract;
use CalderaLearn\RestSearch\FilterQueryArgs;
use CalderaLearn\RestSearch\FilterSchema;
use CalderaLearn\RestSearch\ModifySchemaContract;
class Factory
{
/**
* Create Search system
*
* @param array $argumentsToAdd
* @param array $postTypesToSupport
* @return Search
*/
public static function search(array $argumentsToAdd, array $postTypesToSupport)
{
foreach ($argumentsToAdd as $argumentName => $argument) {
$schemaModify = new class( $postTypesToSupport ) extends FilterSchema {
/**
* @var array
*/
protected $arguments = [];
/**
* @var array
*/
protected $postTypesToSupport = [];
public function __construct(array $postTypesToSupport)
{
$this->postTypesToSupport = $postTypesToSupport;
}
public function addArgument(string $argumentName, array $argument = null)
{
$this->arguments[$argumentName] = $argument;
return $this;
}
public function getAdditionalSchemaArguments(): array
{
return $this->arguments;
}
public function shouldFilter(string $postTypeSlug): bool
{
return in_array($postTypeSlug, $this->postTypesToSupport);
}
};
$queryModify = new class( $postTypesToSupport ) extends FilterQueryArgs {
/**
* @var array
*/
protected $arguments = [];
/**
* @var array
*/
protected $postTypesToSupport = [];
public function __construct(array $postTypesToSupport)
{
$this->postTypesToSupport = $postTypesToSupport;
}
public function addArgument(string $argumentName, $default)
{
$this->arguments[$argumentName] = $default;
return $this;
}
public function getAdditionalQueryArguments(): array
{
return $this->arguments;
}
public function shouldFilter(string $postTypeSlug): bool
{
return in_array($postTypeSlug, $this->postTypesToSupport);
}
};
$schemaModify->addArgument($argumentName, $argument['schema']);
$queryModify->addArgument($argumentName, $argument['default']);
return new Search($queryModify, $schemaModify);
}
}
}

0 comments on commit 3268a62

Please sign in to comment.
You can’t perform that action at this time.