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
!26
feature(ajax): added nonce
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
feature(ajax): added nonce
feature/dismiss-nonce
into
master
Overview
12
Commits
14
Pipelines
15
Changes
10
All threads resolved!
Hide all comments
Merged
Grzegorz Rola
requested to merge
feature/dismiss-nonce
into
master
2 years ago
Overview
12
Commits
14
Pipelines
15
Changes
10
All threads resolved!
Hide all comments
0
0
Merge request reports
Viewing commit
9aa5afc2
Prev
Next
Show latest version
10 files
+
353
−
329
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
10
9aa5afc2
feature(ajax): added nonce
· 9aa5afc2
Grzegorz Rola
authored
2 years ago
tests/codeception/tests/integration/AjaxHandlerTest.php
0 → 100644
+
106
−
0
View file @ 79f0f46d
Edit in single-file editor
Open in Web IDE
<?php
namespace
codeception\tests\integration
;
use
Codeception\TestCase\WPTestCase
;
use
\WPDesk\Notice\AjaxHandler
;
use
\WPDesk\Notice\PermanentDismissibleNotice
;
class
AjaxHandlerTest
extends
WPTestCase
{
const
ASSETS_URL
=
'http://test.com/test/assetes/'
;
const
NOTICE_NAME
=
'test_notice_name'
;
const
WP_DEFAULT_PRIORITY
=
10
;
public
function
setUp
()
{
parent
::
setUp
();
}
public
function
tearDown
()
{
parent
::
tearDown
();
}
public
function
testHooksWithAssetsURL
()
{
$ajaxHandler
=
new
AjaxHandler
(
self
::
ASSETS_URL
);
$ajaxHandler
->
hooks
();
$this
->
assertEquals
(
self
::
WP_DEFAULT_PRIORITY
,
has_action
(
'admin_enqueue_scripts'
,
[
$ajaxHandler
,
'enqueueAdminScripts'
]
)
);
$this
->
assertEquals
(
self
::
WP_DEFAULT_PRIORITY
,
has_action
(
'wp_ajax_wpdesk_notice_dismiss'
,
[
$ajaxHandler
,
'processAjaxNoticeDismiss'
]
)
);
}
public
function
testHooksWithoutAssetsURL
()
{
$ajaxHandler
=
new
AjaxHandler
();
$ajaxHandler
->
hooks
();
$this
->
assertEquals
(
self
::
WP_DEFAULT_PRIORITY
,
has_action
(
'admin_head'
,
[
$ajaxHandler
,
'addScriptToAdminHead'
]
)
);
$this
->
assertEquals
(
self
::
WP_DEFAULT_PRIORITY
,
has_action
(
'wp_ajax_wpdesk_notice_dismiss'
,
[
$ajaxHandler
,
'processAjaxNoticeDismiss'
]
)
);
}
public
function
testEnqueueAdminScripts
()
{
$this
->
markTestSkipped
(
'Must be revisited. get_current_screen not working.'
);
$ajaxHandler
=
new
AjaxHandler
(
self
::
ASSETS_URL
);
$ajaxHandler
->
hooks
();
do_action
(
'admin_enqueue_scripts'
);
$registeredScripts
=
wp_scripts
()
->
registered
;
$this
->
assertArrayHasKey
(
'wpdesk_notice'
,
$registeredScripts
,
'Script not registered!'
);
$this
->
assertEquals
(
self
::
ASSETS_URL
.
'js/notice.js'
,
$registeredScripts
[
'wpdesk_notice'
]
->
src
,
'Script src is invalid!'
);
}
public
function
testAddScriptToAdminHead
()
{
$ajaxHandler
=
new
AjaxHandler
();
$ajaxHandler
->
hooks
();
$this
->
expectOutputString
(
'<script type="text/javascript">'
.
"
\n
"
.
file_get_contents
(
__DIR__
.
'/../../../../assets/js/notice.js'
)
.
'</script>
'
);
$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
)
);
$ajaxHandler
=
new
AjaxHandler
(
self
::
ASSETS_URL
);
$ajaxHandler
->
processAjaxNoticeDismiss
();
$this
->
assertEquals
(
PermanentDismissibleNotice
::
OPTION_VALUE_DISMISSED
,
get_option
(
PermanentDismissibleNotice
::
OPTION_NAME_PREFIX
.
self
::
NOTICE_NAME
)
);
}
public
function
testShoulfNotProcessAjaxNoticeDismissWhenInvalidNonce
()
{
$_POST
[
AjaxHandler
::
POST_FIELD_NOTICE_NAME
]
=
self
::
NOTICE_NAME
;
$_POST
[
AjaxHandler
::
POST_FIELD_SECURITY
]
=
wp_create_nonce
();
$ajaxHandler
=
new
AjaxHandler
(
self
::
ASSETS_URL
);
$ajaxHandler
->
processAjaxNoticeDismiss
();
$this
->
assertNotEquals
(
PermanentDismissibleNotice
::
OPTION_VALUE_DISMISSED
,
get_option
(
PermanentDismissibleNotice
::
OPTION_NAME_PREFIX
.
self
::
NOTICE_NAME
)
);
}
}
Loading