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
c9a5cb20
Commit
c9a5cb20
authored
May 26, 2019
by
Erick Hitter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests for setting fields and sanitization
parent
e6b538d7
Pipeline
#1060
failed with stages
in 2 minutes and 53 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
262 additions
and
12 deletions
+262
-12
inc/class-wp-revisions-control.php
inc/class-wp-revisions-control.php
+19
-5
tests/bootstrap.php
tests/bootstrap.php
+7
-0
tests/test-misc.php
tests/test-misc.php
+48
-0
tests/test-purges.php
tests/test-purges.php
+0
-7
tests/test-ui.php
tests/test-ui.php
+188
-0
No files found.
inc/class-wp-revisions-control.php
View file @
c9a5cb20
...
@@ -505,10 +505,12 @@ class WP_Revisions_Control {
...
@@ -505,10 +505,12 @@ class WP_Revisions_Control {
* @return array
* @return array
*/
*/
private
function
get_settings
()
{
private
function
get_settings
()
{
if
(
empty
(
self
::
$settings
)
)
{
static
$hash
=
null
;
$post_types
=
$this
->
get_post_types
();
$settings
=
get_option
(
$this
->
settings_section
,
array
()
);
$settings
=
get_option
(
$this
->
settings_section
,
array
()
);
if
(
empty
(
self
::
$settings
)
||
$hash
!==
$this
->
hash_settings
(
$settings
)
)
{
$post_types
=
$this
->
get_post_types
();
if
(
!
is_array
(
$settings
)
)
{
if
(
!
is_array
(
$settings
)
)
{
$settings
=
array
();
$settings
=
array
();
...
@@ -525,11 +527,23 @@ class WP_Revisions_Control {
...
@@ -525,11 +527,23 @@ class WP_Revisions_Control {
}
}
self
::
$settings
=
$merged_settings
;
self
::
$settings
=
$merged_settings
;
$hash
=
$this
->
hash_settings
(
self
::
$settings
);
}
}
return
self
::
$settings
;
return
self
::
$settings
;
}
}
/**
* Hash settings to limit re-parsing.
*
* @param array $settings Settings array.
* @return string
*/
private
function
hash_settings
(
$settings
)
{
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
return
md5
(
serialize
(
$settings
)
);
}
/**
/**
* Retrieve array of supported post types and their labels.
* Retrieve array of supported post types and their labels.
*
*
...
@@ -572,7 +586,7 @@ class WP_Revisions_Control {
...
@@ -572,7 +586,7 @@ class WP_Revisions_Control {
$_post
=
new
WP_Post
(
(
object
)
array
(
'post_type'
=>
$post_type
)
);
$_post
=
new
WP_Post
(
(
object
)
array
(
'post_type'
=>
$post_type
)
);
$to_keep
=
wp_revisions_to_keep
(
$_post
);
$to_keep
=
wp_revisions_to_keep
(
$_post
);
if
(
$blank_for_all
&&
-
1
===
$to_keep
)
{
if
(
$blank_for_all
&&
(
-
1
===
$to_keep
||
'-1'
===
$to_keep
)
)
{
return
''
;
return
''
;
}
else
{
}
else
{
return
(
int
)
$to_keep
;
return
(
int
)
$to_keep
;
...
@@ -588,7 +602,7 @@ class WP_Revisions_Control {
...
@@ -588,7 +602,7 @@ class WP_Revisions_Control {
private
function
get_post_revisions_to_keep
(
$post_id
)
{
private
function
get_post_revisions_to_keep
(
$post_id
)
{
$to_keep
=
get_post_meta
(
$post_id
,
$this
->
meta_key_limit
,
true
);
$to_keep
=
get_post_meta
(
$post_id
,
$this
->
meta_key_limit
,
true
);
if
(
-
1
===
$to_keep
||
empty
(
$to_keep
)
)
{
if
(
empty
(
$to_keep
)
||
-
1
===
$to_keep
||
'-1'
===
$to_keep
)
{
$to_keep
=
''
;
$to_keep
=
''
;
}
else
{
}
else
{
$to_keep
=
(
int
)
$to_keep
;
$to_keep
=
(
int
)
$to_keep
;
...
...
tests/bootstrap.php
View file @
c9a5cb20
...
@@ -19,6 +19,13 @@ if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
...
@@ -19,6 +19,13 @@ if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
// Give access to tests_add_filter() function.
// Give access to tests_add_filter() function.
require_once
$_tests_dir
.
'/includes/functions.php'
;
require_once
$_tests_dir
.
'/includes/functions.php'
;
/**
* Stub admin-only function not needed for testing.
*/
if
(
!
function_exists
(
'post_revisions_meta_box'
)
)
{
function
post_revisions_meta_box
()
{}
}
/**
/**
* Manually load the plugin being tested.
* Manually load the plugin being tested.
*/
*/
...
...
tests/test-misc.php
0 → 100755
View file @
c9a5cb20
<?php
/**
* Test miscellaneous methods.
*
* @package WP_Revisions_Control
*/
/**
* Class TestMisc.
*/
class
TestMisc
extends
WP_UnitTestCase
{
/**
* Test settings sanitization.
*/
public
function
test_settings_sanitization
()
{
$input
=
array
(
'minus_ten'
=>
-
10
,
'minus_one'
=>
-
1
,
'zero'
=>
0
,
'one'
=>
1
,
'thirty'
=>
30
,
'empty_string'
=>
''
,
'bool_false'
=>
false
,
'bool_true'
=>
true
,
'null'
=>
null
,
);
$expected
=
array
(
'minus_ten'
=>
-
1
,
'minus_one'
=>
-
1
,
'zero'
=>
0
,
'one'
=>
1
,
'thirty'
=>
30
,
'empty_string'
=>
-
1
,
'bool_false'
=>
-
1
,
'bool_true'
=>
1
,
'null'
=>
-
1
,
);
$sanitized
=
WP_Revisions_Control
::
get_instance
()
->
sanitize_options
(
$input
);
$this
->
assertEquals
(
$expected
,
$sanitized
,
'Failed to assert that options were sanitized correctly.'
);
}
}
tests/test-purges.php
View file @
c9a5cb20
...
@@ -9,13 +9,6 @@
...
@@ -9,13 +9,6 @@
* Class TestPurges.
* Class TestPurges.
*/
*/
class
TestPurges
extends
WP_UnitTestCase
{
class
TestPurges
extends
WP_UnitTestCase
{
/**
* Plugin slug used in many settings etc.
*
* @var string
*/
protected
static
$settings_section
=
'wp_revisions_control'
;
/**
/**
* Plugin's limit meta key.
* Plugin's limit meta key.
*
*
...
...
tests/test-ui.php
0 → 100755
View file @
c9a5cb20
<?php
/**
* Test UI methods.
*
* @package WP_Revisions_Control
*/
/**
* Class TestUI.
*/
class
TestUI
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 meta box with no meta set.
*/
public
function
test_no_meta
()
{
$post_id
=
$this
->
factory
->
post
->
create
();
ob_start
();
WP_Revisions_Control
::
get_instance
()
->
revisions_meta_box
(
get_post
(
$post_id
)
);
$meta_box
=
ob_get_clean
();
$this
->
assertContains
(
'value=""'
,
$meta_box
,
'Failed to assert that meta box has no value when no setting exists.'
);
}
/**
* Test meta box with no limit set.
*/
public
function
test_no_limit
()
{
$post_id
=
$this
->
factory
->
post
->
create
();
update_post_meta
(
$post_id
,
static
::
$meta_key
,
-
1
);
ob_start
();
WP_Revisions_Control
::
get_instance
()
->
revisions_meta_box
(
get_post
(
$post_id
)
);
$meta_box
=
ob_get_clean
();
$this
->
assertContains
(
'value=""'
,
$meta_box
,
'Failed to assert that meta box has no value when no limit exists.'
);
}
/**
* Test settings-field output when no options are set.
*/
public
function
test_settings_fields_no_options
()
{
ob_start
();
WP_Revisions_Control
::
get_instance
()
->
field_post_type
(
array
(
'post_type'
=>
'post'
)
);
$post_field
=
ob_get_clean
();
ob_start
();
WP_Revisions_Control
::
get_instance
()
->
field_post_type
(
array
(
'post_type'
=>
'page'
)
);
$page_field
=
ob_get_clean
();
$name_format
=
'name="%1$s[%2$s]"'
;
$value_format
=
'value=""'
;
$this
->
assertContains
(
sprintf
(
$name_format
,
static
::
$settings_section
,
'post'
),
$post_field
,
'Failed to assert that post field had correct name for post type.'
);
$this
->
assertContains
(
sprintf
(
$name_format
,
static
::
$settings_section
,
'page'
),
$page_field
,
'Failed to assert that page field had correct name for post type.'
);
$this
->
assertContains
(
$value_format
,
$post_field
,
'Failed to assert that post field had correct value.'
);
$this
->
assertContains
(
$value_format
,
$page_field
,
'Failed to assert that page field had correct value.'
);
}
/**
* Test settings-field output when options are set.
*/
public
function
test_settings_fields_with_options
()
{
$value
=
12
;
update_option
(
static
::
$settings_section
,
[
'post'
=>
$value
,
]
);
ob_start
();
WP_Revisions_Control
::
get_instance
()
->
field_post_type
(
array
(
'post_type'
=>
'post'
)
);
$post_field
=
ob_get_clean
();
$name_format
=
'name="%1$s[%2$s]"'
;
$value_format
=
'value="%1$s"'
;
$this
->
assertContains
(
sprintf
(
$name_format
,
static
::
$settings_section
,
'post'
),
$post_field
,
'Failed to assert that post field had correct name for post type.'
);
$this
->
assertContains
(
sprintf
(
$value_format
,
$value
),
$post_field
,
'Failed to assert that post field had correct value.'
);
}
/**
* Test settings-field output when options are set.
*/
public
function
test_settings_fields_with_options_keep_all
()
{
$value
=
-
1
;
update_option
(
static
::
$settings_section
,
[
'post'
=>
$value
,
]
);
ob_start
();
WP_Revisions_Control
::
get_instance
()
->
field_post_type
(
array
(
'post_type'
=>
'post'
)
);
$post_field
=
ob_get_clean
();
$name_format
=
'name="%1$s[%2$s]"'
;
$value_format
=
'value=""'
;
$this
->
assertContains
(
sprintf
(
$name_format
,
static
::
$settings_section
,
'post'
),
$post_field
,
'Failed to assert that post field had correct name for post type.'
);
$this
->
assertContains
(
sprintf
(
$value_format
,
$value
),
$post_field
,
'Failed to assert that post field had correct value.'
);
}
}
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