Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
W
WP Revisions Control
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Code Review
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
WP Plugins
WP Revisions Control
Commits
7670551f
Commit
7670551f
authored
May 26, 2019
by
Erick Hitter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests for limits and purge-all function.
parent
9659f242
Pipeline
#1053
failed with stages
in 3 minutes and 4 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
37 deletions
+141
-37
inc/class-wp-revisions-control.php
inc/class-wp-revisions-control.php
+31
-17
tests/test-hooks.php
tests/test-hooks.php
+110
-0
tests/test-sample.php
tests/test-sample.php
+0
-20
No files found.
inc/class-wp-revisions-control.php
View file @
7670551f
...
...
@@ -362,29 +362,43 @@ class WP_Revisions_Control {
// Request is valid if $response is still empty, as no errors arose above.
if
(
empty
(
$response
)
)
{
$revisions
=
wp_get_post_revisions
(
$post_id
);
$response
=
$this
->
do_purge_all
(
$post_id
);
}
$count
=
count
(
$revisions
);
// Pass the response back to JS.
echo
json_encode
(
$response
);
exit
;
}
foreach
(
$revisions
as
$revision
)
{
wp_delete_post_revision
(
$revision
->
ID
);
}
/**
* Remove all revisions from a given post ID.
*
* @param int $post_id Post ID to purge of revisions.
* @return array
*/
public
function
do_purge_all
(
$post_id
)
{
$response
=
array
();
$response
[
'success'
]
=
sprintf
(
/* translators: 1. Number of removed revisions, already formatted for locale. */
esc_html__
(
'Removed %1$s revisions associated with this post.'
,
'wp_revisions_control'
),
number_format_i18n
(
$count
,
0
)
);
$revisions
=
wp_get_post_revisions
(
$post_id
);
$count
=
count
(
$revisions
);
$response
[
'count'
]
=
$count
;
foreach
(
$revisions
as
$revision
)
{
wp_delete_post_revision
(
$revision
->
ID
);
}
// Pass the response back to JS.
echo
json_encode
(
$response
);
exit
;
$response
[
'success'
]
=
sprintf
(
/* translators: 1. Number of removed revisions, already formatted for locale. */
esc_html__
(
'Removed %1$s revisions associated with this post.'
,
'wp_revisions_control'
),
number_format_i18n
(
$count
,
0
)
);
$response
[
'count'
]
=
$count
;
return
$response
;
}
/**
...
...
tests/test-hooks.php
0 → 100755
View file @
7670551f
<?php
/**
* Test WP hooks.
*
* @package WP_Revisions_Control
*/
/**
* Class TestHooks.
*/
class
TestHooks
extends
WP_UnitTestCase
{
/**
* Plugin slug used in many settings etc.
*
* @var string
*/
protected
static
$settings_section
=
'wp_revisions_control'
;
/**
* Plugin's limit meta key.
*
* @var string
*/
protected
static
$meta_key
=
'_wp_rev_ctl_limit'
;
/**
* Test saving post's revisions limit.
*/
public
function
test_save_post
()
{
$post_id
=
$this
->
factory
->
post
->
create
();
$expected
=
92
;
$_POST
[
static
::
$settings_section
.
'_limit_nonce'
]
=
wp_create_nonce
(
static
::
$settings_section
.
'_limit'
);
$_POST
[
static
::
$settings_section
.
'_qty'
]
=
$expected
;
WP_Revisions_Control
::
get_instance
()
->
action_save_post
(
$post_id
);
$to_keep
=
(
int
)
get_post_meta
(
$post_id
,
static
::
$meta_key
,
true
);
$to_keep_filtered
=
wp_revisions_to_keep
(
get_post
(
$post_id
)
);
$this
->
assertEquals
(
$expected
,
$to_keep
);
$this
->
assertEquals
(
$expected
,
$to_keep_filtered
);
}
/**
* Test limits, ensuring no leakage.
*/
public
function
test_limits
()
{
$post_id_limited
=
$this
->
factory
->
post
->
create
();
$post_id_unlimited
=
$this
->
factory
->
post
->
create
();
$expected
=
47
;
update_post_meta
(
$post_id_limited
,
static
::
$meta_key
,
$expected
);
$this
->
assertEquals
(
$expected
,
wp_revisions_to_keep
(
get_post
(
$post_id_limited
)
)
);
$this
->
assertEquals
(
-
1
,
wp_revisions_to_keep
(
get_post
(
$post_id_unlimited
)
)
);
}
/**
* Test revision purging.
*/
public
function
test_purge_all
()
{
$post_id
=
$this
->
factory
->
post
->
create
();
$iterations
=
10
;
for
(
$i
=
0
;
$i
<
$iterations
;
$i
++
)
{
wp_update_post
(
[
'ID'
=>
$post_id
,
'post_content'
=>
wp_rand
(),
]
);
}
$revisions_to_purge
=
count
(
wp_get_post_revisions
(
$post_id
)
);
$this
->
assertEquals
(
$iterations
,
$revisions_to_purge
,
'Failed to assert that there are revisions to purge.'
);
$purge
=
WP_Revisions_Control
::
get_instance
()
->
do_purge_all
(
$post_id
);
$revisions_remaining
=
count
(
wp_get_post_revisions
(
$post_id
)
);
$this
->
assertEquals
(
0
,
$revisions_remaining
,
'Failed to assert that all revisions were purged.'
);
$this
->
assertEquals
(
10
,
$purge
[
'count'
],
'Failed to assert that response includes expected count of purged revisions.'
);
$this
->
assertEquals
(
'Removed 10 revisions associated with this post.'
,
$purge
[
'success'
],
'Failed to assert that response includes expected success message.'
);
}
}
tests/test-sample.php
deleted
100755 → 0
View file @
9659f242
<?php
/**
* Class SampleTest
*
* @package WP_Revisions_Control
*/
/**
* 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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment