Skip to content
Snippets Groups Projects
Commit 2cdddd43 authored by Erick Hitter's avatar Erick Hitter
Browse files

Add tests

parent 0887be4b
No related branches found
No related tags found
1 merge request!2v0.3
......@@ -23,6 +23,9 @@ require_once $_tests_dir . '/includes/functions.php';
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
// Plugin requires pretty permalinks to function.
_set_default_permalink_structure_for_tests();
require dirname( dirname( __FILE__ ) ) . '/eth-redirect-to-latest.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
......
<?php
/**
* Class PluginTest.
*
* @package ETH_Redirect_To_Latest_Post
*/
/**
* Plugin test case.
*/
class PluginTest extends WP_UnitTestCase {
/**
* ID of latest post.
*
* @var int
*/
protected static $latest_id;
/**
* Create some objects with various dates, so something
* can be considered "latest."
*
* We create an assortment of posts and pages with the
* slug used for redirection so that we can confirm that
* redirection takes priority.
*/
public function setUp() {
parent::setUp();
$this->factory->post->create_many(
10,
array(
'post_date' => date( 'Y-m-d H:i:s', strtotime( '-1 minute' ) ),
'post_name' => 'latest',
)
);
$this->factory->post->create_many(
10,
array(
'post_date' => date( 'Y-m-d H:i:s', strtotime( '-1 hour' ) ),
'post_name' => 'latest',
)
);
$this->factory->post->create_many(
10,
array(
'post_date' => date( 'Y-m-d H:i:s', strtotime( '-1 minute' ) ),
'post_type' => 'page',
'post_name' => 'latest',
)
);
$this->factory->post->create_many(
10,
array(
'post_date' => date( 'Y-m-d H:i:s', strtotime( '-1 hour' ) ),
'post_type' => 'page',
'post_name' => 'latest',
)
);
static::$latest_id = $this->factory->post->create();
}
/**
* Test function that retrieves latest post when
* requested as a non-hierarchical permastruct.
*/
public function test_get_latest_non_hierarchical() {
$fake_request = new stdClass();
$fake_request->query_vars = array(
'name' => 'latest',
);
$this->assert_for_request( $fake_request );
}
/**
* Test function that retrieves latest post when
* requested as a non-hierarchical permastruct.
*/
public function test_get_latest_hierarchical() {
$fake_request = new stdClass();
$fake_request->query_vars = array(
'pagename' => 'latest',
);
$this->assert_for_request( $fake_request );
}
/**
* Helper for shared assertions.
*
* @param stdClass $fake_request Faux WP object.
*/
protected function assert_for_request( $fake_request ) {
$redirect = ETH_Redirect_To_Latest_Post::get_instance()->get_redirect_for_request( $fake_request );
$this->assertEquals(
get_permalink( static::$latest_id ),
$redirect->destination,
'Failed to assert that redirect destination is permalink of latest post.'
);
$this->assertEquals(
302,
$redirect->status_code,
'Failed to assert that redirect status code is for a temporary redirect.'
);
}
/**
* Test that redirection doesn't return a post with
* the slug that's also used for redirection.
*/
public function test_not_matching_post_name() {
$fake_request = new stdClass();
$fake_request->query_vars = array(
'name' => 'latest',
);
$redirect = ETH_Redirect_To_Latest_Post::get_instance()->get_redirect_for_request( $fake_request );
$object_by_slug = get_posts(
[
'posts_per_page' => 1,
'post_status' => 'any',
'name' => $fake_request->query_vars['name'],
]
);
$this->assertCount(
1,
$object_by_slug,
'Failed to assert that a post exists with the slug used for redirection.'
);
$object_by_slug = array_shift( $object_by_slug );
$this->assertNotEquals(
get_permalink( $object_by_slug ),
$redirect->destination,
'Failed to assert that latest post is not the object with the slug "latest."'
);
}
/**
* Test that redirection doesn't return a post with
* the slug that's also used for redirection.
*/
public function test_not_matching_page_name() {
$fake_request = new stdClass();
$fake_request->query_vars = array(
'pagename' => 'latest',
);
$redirect = ETH_Redirect_To_Latest_Post::get_instance()->get_redirect_for_request( $fake_request );
$object_by_slug = get_posts(
[
'posts_per_page' => 1,
'post_status' => 'any',
'post_type' => 'page',
'pagename' => $fake_request->query_vars['pagename'],
]
);
$this->assertCount(
1,
$object_by_slug,
'Failed to assert that a page exists with the slug used for redirection.'
);
$object_by_slug = array_shift( $object_by_slug );
$this->assertNotEquals(
get_permalink( $object_by_slug ),
$redirect->destination,
'Failed to assert that latest post is not the object with the slug "latest."'
);
}
}
<?php
/**
* Class SampleTest
*
* @package ETH_Redirect_To_Latest
*/
/**
* Sample test case.
*/
class SampleTest extends WP_UnitTestCase {
/**
* A single example test.
*/
public function test_sample() {
// Replace this with some actual testing code.
$this->assertTrue( true );
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment