diff options
author | Daniel Weipert <code@drogueronin.de> | 2020-12-21 16:11:17 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2020-12-21 16:11:17 +0100 |
commit | 0d1da4356173e926fdcac42462daa4fcb7617109 (patch) | |
tree | 79535a3dca38b60c1ca93ff351c00e1464b0536b /tests |
Initial commit
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bootstrap.php | 31 | ||||
-rw-r--r-- | tests/test-post-order.php | 109 |
2 files changed, 140 insertions, 0 deletions
diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..67d85f5 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,31 @@ +<?php +/** + * PHPUnit bootstrap file + * + * @package Draggable_Post_Order + */ + +$_tests_dir = getenv( 'WP_TESTS_DIR' ); + +if ( ! $_tests_dir ) { + $_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; +} + +if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) { + echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + exit( 1 ); +} + +// Give access to tests_add_filter() function. +require_once $_tests_dir . '/includes/functions.php'; + +/** + * Manually load the plugin being tested. + */ +function _manually_load_plugin() { + require dirname( dirname( __FILE__ ) ) . '/draggable-post-order.php'; +} +tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); + +// Start up the WP testing environment. +require $_tests_dir . '/includes/bootstrap.php'; diff --git a/tests/test-post-order.php b/tests/test-post-order.php new file mode 100644 index 0000000..b5b26c7 --- /dev/null +++ b/tests/test-post-order.php @@ -0,0 +1,109 @@ +<?php +/** + * Class TestPostOrder + * + * @package DraggablePostOrder + */ + +/** + * Sample test case. + */ +class TestPostOrder extends WP_UnitTestCase { + + /** + * The post type for testing. + * + * @var WP_Post_Type + */ + public WP_Post_Type $test_post_type; + + /** + * Runs the routine before each test is executed. + */ + public function setUp(): void { + parent::setUp(); + + $this->test_post_type = register_post_type( + 'test', + [ + 'supports' => [ 'draggable-post-order' ], + 'has_archive' => true, + ] + ); + + // since tests are loaded after init, run init manually. + \Draggable_Post_Order\Draggable_Post_Order::init(); + } + + /** + * Test query manipulation on meta_query + */ + public function test_query_manipulation_meta_query() { + $this->go_to( '/?post_type=test' ); + $wp_query = $GLOBALS['wp_query']; + + $meta_sub_query = [ + 'relation' => 'OR', + 'draggable-post-order-clause' => [ + 'key' => 'draggable-post-order', + 'type' => 'NUMERIC', + ], + // get all posts without the meta as well. + 'draggable-post-order-exists-clause' => [ + 'key' => 'draggable-post-order', + 'compare' => 'NOT EXISTS', + ], + ]; + $this->assertEquals( [ $meta_sub_query ], $wp_query->get( 'meta_query' ) ); + } + + /** + * Test query manipulation on orderby with ASC + */ + public function test_query_manipulation_orderby_asc() { + $this->go_to( '/?post_type=test' ); + $wp_query = $GLOBALS['wp_query']; + + $orderby = [ + 'draggable-post-order-clause' => 'ASC', + 'draggable-post-order-exists-clause' => 'ASC', + ]; + $this->assertEquals( $orderby, $wp_query->get( 'orderby' ) ); + } + + /** + * Test query manipulation on orderby with DESC + */ + public function test_query_manipulation_orderby_desc() { + tests_add_filter( + 'draggable_post_order_order', + function ( $order, $post_type, $query ) { + $this->assertEquals( 'ASC', $order ); + $this->assertEquals( 'test', $post_type ); + $this->assertTrue( $query->is_archive ); + + return 'DESC'; + }, + 10, + 3 + ); + $this->go_to( '/?post_type=test' ); + $wp_query = $GLOBALS['wp_query']; + + $orderby = [ + 'draggable-post-order-clause' => 'DESC', + 'draggable-post-order-exists-clause' => 'DESC', + ]; + $this->assertEquals( $orderby, $wp_query->get( 'orderby' ) ); + } + + /** + * Test that query is not manipulated on unrelated pages + */ + public function test_query_manipulation_orderby_none() { + $this->go_to( '/' ); + $wp_query = $GLOBALS['wp_query']; + + $this->assertEmpty( $wp_query->get( 'orderby' ) ); + } +} |