Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
External Permalinks Redux
Manage
Activity
Members
Labels
Plan
Issues
2
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
0
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
External Permalinks Redux
Commits
7652c793
Commit
7652c793
authored
5 years ago
by
Erick Hitter
Browse files
Options
Downloads
Patches
Plain Diff
Abstract redirect lookup so it can be tested
parent
747088e2
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!3
Introduce unit tests
Pipeline
#879
passed with warnings with stages
in 3 minutes and 25 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
external-permalinks-redux.php
+33
-11
33 additions, 11 deletions
external-permalinks-redux.php
tests/test-redirect-callbacks.php
+94
-0
94 additions, 0 deletions
tests/test-redirect-callbacks.php
with
127 additions
and
11 deletions
external-permalinks-redux.php
+
33
−
11
View file @
7652c793
...
@@ -230,21 +230,43 @@ class external_permalinks_redux {
...
@@ -230,21 +230,43 @@ class external_permalinks_redux {
return
;
return
;
}
}
$
link
=
get_post_meta
(
$post
->
ID
,
$this
->
meta_key_target
,
true
);
$
redirect
=
$this
->
get_redirect_data
(
$post
->
ID
);
if
(
!
empty
(
$link
)
)
{
if
(
false
===
$redirect
)
{
$type
=
(
int
)
get_post_meta
(
$post
->
ID
,
$this
->
meta_key_type
,
true
)
;
return
;
$type
=
apply_filters
(
'epr_status_code'
,
$type
,
$link
,
$post
);
}
if
(
!
$type
)
{
// Unreasonable to validate redirect destination.
$type
=
302
;
// phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
}
wp_redirect
(
$redirect
[
'link'
],
$redirect
[
'type'
]
);
exit
;
}
// Unreasonable to validate redirect destination.
/**
// phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
* Retrieve redirect data for a given post ID.
wp_redirect
(
$link
,
$type
);
*
exit
;
* @param int $post_id Post ID.
* @return array|bool
*/
public
function
get_redirect_data
(
$post_id
)
{
if
(
!
is_numeric
(
$post_id
)
)
{
return
false
;
}
}
$link
=
get_post_meta
(
$post_id
,
$this
->
meta_key_target
,
true
);
if
(
empty
(
$link
)
)
{
return
false
;
}
$type
=
(
int
)
get_post_meta
(
$post_id
,
$this
->
meta_key_type
,
true
);
$type
=
apply_filters
(
'epr_status_code'
,
$type
,
$link
,
get_post
(
$post_id
)
);
if
(
!
$type
)
{
$type
=
302
;
}
return
compact
(
'link'
,
'type'
);
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
tests/test-redirect-callbacks.php
0 → 100755
+
94
−
0
View file @
7652c793
<?php
/**
* Class RedirectCallbacks
*
* @package External_Permalinks_Redux
*/
/**
* Test redirect callbacks
*/
class
RedirectCallbacks
extends
WP_UnitTestCase
{
/**
* Redirect destination.
*/
const
DESTINATION
=
'https://w.org/'
;
/**
* Plugin instance.
*
* @var external_permalinks_redux
*/
protected
$plugin
;
/**
* Create some objects with redirects.
*/
public
function
setUp
()
{
parent
::
setUp
();
$this
->
plugin
=
external_permalinks_redux
::
get_instance
();
}
/**
* Helper to retrieve a clean post.
*
* @return int
*/
protected
function
get_new_post
()
{
return
$this
->
factory
->
post
->
create
(
[
'post_type'
=>
'post'
,
]
);
}
/**
* Test post with default redirect code.
*/
public
function
test_post_redirect_default_status
()
{
$post_id
=
$this
->
get_new_post
();
update_post_meta
(
$post_id
,
$this
->
plugin
->
meta_key_target
,
static
::
DESTINATION
);
$redirect
=
$this
->
plugin
->
get_redirect_data
(
$post_id
);
$this
->
assertEquals
(
static
::
DESTINATION
,
$redirect
[
'link'
]
);
$this
->
assertEquals
(
302
,
$redirect
[
'type'
]
);
}
/**
* test post with custom redirect code.
*/
public
function
test_post_redirect_custom_status
()
{
$post_id
=
$this
->
get_new_post
();
update_post_meta
(
$post_id
,
$this
->
plugin
->
meta_key_target
,
static
::
DESTINATION
);
update_post_meta
(
$post_id
,
$this
->
plugin
->
meta_key_type
,
307
);
$redirect
=
$this
->
plugin
->
get_redirect_data
(
$post_id
);
$this
->
assertEquals
(
static
::
DESTINATION
,
$redirect
[
'link'
]
);
$this
->
assertEquals
(
307
,
$redirect
[
'type'
]
);
}
/**
* Test post with redirect type but no destination.
*/
public
function
test_post_redirect_missing_destination
()
{
$post_id
=
$this
->
get_new_post
();
update_post_meta
(
$post_id
,
$this
->
plugin
->
meta_key_type
,
307
);
$redirect
=
$this
->
plugin
->
get_redirect_data
(
$post_id
);
$this
->
assertFalse
(
$redirect
);
}
/**
* Test post without redirect.
*/
public
function
test_post_no_redirect
()
{
$post_id
=
$this
->
get_new_post
();
$redirect
=
$this
->
plugin
->
get_redirect_data
(
$post_id
);
$this
->
assertFalse
(
$redirect
);
}
}
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