Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
wp-notice
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
wpdesk
wp-notice
Merge requests
!28
bugfix(ajax): permission check
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
bugfix(ajax): permission check
bugfix/permission-check
into
master
Overview
0
Commits
8
Pipelines
8
Changes
1
Merged
Grzegorz Rola
requested to merge
bugfix/permission-check
into
master
1 year ago
Overview
0
Commits
8
Pipelines
8
Changes
1
0
0
Merge request reports
Viewing commit
13f847f8
Prev
Next
Show latest version
1 file
+
36
−
11
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
13f847f8
bugfix(ajax): permission check
· 13f847f8
Grzegorz Rola
authored
1 year ago
tests/codeception/tests/integration/AjaxHandlerTest.php
+
36
−
11
View file @ 13f847f8
Edit in single-file editor
Open in Web IDE
Show full file
@@ -14,12 +14,22 @@ class AjaxHandlerTest extends WPTestCase {
public
function
setUp
()
{
parent
::
setUp
();
add_filter
(
'wp_doing_ajax'
,
'__return_true'
);
add_filter
(
'wp_die_ajax_handler'
,
array
(
$this
,
'getDieHandler'
),
1
,
1
);
}
public
function
tearDown
()
{
parent
::
tearDown
();
}
public
function
getDieHandler
(
$handler
)
{
return
array
(
$this
,
'dieHandler'
);
}
public
function
dieHandler
(
$message
,
$title
,
$args
)
{
throw
new
\Exception
(
$message
);
}
public
function
testHooksWithAssetsURL
()
{
$ajaxHandler
=
new
AjaxHandler
(
self
::
ASSETS_URL
);
$ajaxHandler
->
hooks
();
@@ -77,25 +87,40 @@ class AjaxHandlerTest extends WPTestCase {
$ajaxHandler
->
addScriptToAdminHead
();
}
public
function
testProcessAjaxNoticeDismiss
()
{
$_POST
[
AjaxHandler
::
POST_FIELD_NOTICE_NAME
]
=
self
::
NOTICE_NAME
;
$_POST
[
AjaxHandler
::
POST_FIELD_SECURITY
]
=
wp_create_nonce
(
PermanentDismissibleNotice
::
OPTION_NAME_PREFIX
.
sanitize_text_field
(
self
::
NOTICE_NAME
)
);
public
function
testProcessAjaxNoticeDismiss
()
{
$user_name
=
'test_user'
;
$random_password
=
wp_generate_password
(
$length
=
12
,
$include_standard_special_chars
=
false
);
$user_email
=
'test@wpdesk.dev'
;
$user_id
=
wp_create_user
(
$user_name
,
$random_password
,
$user_email
);
$user
=
new
\WP_User
(
$user_id
);
$user
->
set_role
(
'administrator'
);
$user
->
save
();
wp_set_current_user
(
$user_id
);
$_POST
[
AjaxHandler
::
POST_FIELD_NOTICE_NAME
]
=
self
::
NOTICE_NAME
;
$_REQUEST
[
AjaxHandler
::
POST_FIELD_SECURITY
]
=
wp_create_nonce
(
PermanentDismissibleNotice
::
OPTION_NAME_PREFIX
.
sanitize_text_field
(
self
::
NOTICE_NAME
)
);
$ajaxHandler
=
new
AjaxHandler
(
self
::
ASSETS_URL
);
$ajaxHandler
->
processAjaxNoticeDismiss
();
$ajaxHandler
->
processAjaxNoticeDismiss
();
$this
->
assertEquals
(
PermanentDismissibleNotice
::
OPTION_VALUE_DISMISSED
,
get_option
(
PermanentDismissibleNotice
::
OPTION_NAME_PREFIX
.
self
::
NOTICE_NAME
)
);
}
$this
->
assertEquals
(
PermanentDismissibleNotice
::
OPTION_VALUE_DISMISSED
,
get_option
(
PermanentDismissibleNotice
::
OPTION_NAME_PREFIX
.
self
::
NOTICE_NAME
,
'0'
)
);
wp_delete_user
(
$user_id
);
}
public
function
testShoulfNotProcessAjaxNoticeDismissWhenInvalidNonce
()
{
$_POST
[
AjaxHandler
::
POST_FIELD_NOTICE_NAME
]
=
self
::
NOTICE_NAME
;
$_
PO
ST
[
AjaxHandler
::
POST_FIELD_SECURITY
]
=
wp_create_nonce
();
$_
REQUE
ST
[
AjaxHandler
::
POST_FIELD_SECURITY
]
=
wp_create_nonce
();
$ajaxHandler
=
new
AjaxHandler
(
self
::
ASSETS_URL
);
$ajaxHandler
->
processAjaxNoticeDismiss
();
try
{
$ajaxHandler
->
processAjaxNoticeDismiss
();
}
catch
(
\Exception
$e
)
{
$this
->
assertEquals
(
'-1'
,
$e
->
getMessage
());
}
$this
->
assertNotEquals
(
PermanentDismissibleNotice
::
OPTION_VALUE_DISMISSED
,
Loading