Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
ETH Redirect to Latest Post
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
WP Plugins
ETH Redirect to Latest Post
Commits
2cdddd43
Commit
2cdddd43
authored
6 years ago
by
Erick Hitter
Browse files
Options
Downloads
Patches
Plain Diff
Add tests
parent
0887be4b
No related branches found
No related tags found
1 merge request
!2
v0.3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
tests/bootstrap.php
+3
-0
3 additions, 0 deletions
tests/bootstrap.php
tests/test-plugin.php
+184
-0
184 additions, 0 deletions
tests/test-plugin.php
tests/test-sample.php
+0
-20
0 additions, 20 deletions
tests/test-sample.php
with
187 additions
and
20 deletions
tests/bootstrap.php
+
3
−
0
View file @
2cdddd43
...
@@ -23,6 +23,9 @@ require_once $_tests_dir . '/includes/functions.php';
...
@@ -23,6 +23,9 @@ require_once $_tests_dir . '/includes/functions.php';
* Manually load the plugin being tested.
* Manually load the plugin being tested.
*/
*/
function
_manually_load_plugin
()
{
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'
;
require
dirname
(
dirname
(
__FILE__
)
)
.
'/eth-redirect-to-latest.php'
;
}
}
tests_add_filter
(
'muplugins_loaded'
,
'_manually_load_plugin'
);
tests_add_filter
(
'muplugins_loaded'
,
'_manually_load_plugin'
);
...
...
This diff is collapsed.
Click to expand it.
tests/test-plugin.php
0 → 100755
+
184
−
0
View file @
2cdddd43
<?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."'
);
}
}
This diff is collapsed.
Click to expand it.
tests/test-sample.php
deleted
100755 → 0
+
0
−
20
View file @
0887be4b
<?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
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment