Skip to content
Snippets Groups Projects
Commit 2262bdcc authored by Grzegorz Rola's avatar Grzegorz Rola
Browse files

feature(ajax): added nonce

parent c0b65c65
No related branches found
No related tags found
1 merge request!26feature(ajax): added nonce
Pipeline #166092 failed
<?php
namespace codeception\tests\integration;
use Codeception\TestCase\WPTestCase;
use \WPDesk\Notice\AjaxHandler;
use \WPDesk\Notice\PermanentDismissibleNotice;
class TestAjaxHandler 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">
' . file_get_contents( __DIR__ . '/../../assets/js/notice.js' ) . '
</script>
'
);
$ajaxHandler->addScriptToAdminHead();
}
public function testProcessAjaxNoticeDismiss() {
$_POST[ AjaxHandler::POST_FIELD_NOTICE_NAME ] = 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 )
);
}
}
<?php
use \WPDesk\Notice\AjaxHandler;
use \WPDesk\Notice\PermanentDismissibleNotice;
class TestAjaxHandler extends WP_UnitTestCase
{
const ASSETS_URL = 'http://test.com/test/assetes/';
const NOTICE_NAME = 'test_notice_name';
const WP_DEFAULT_PRIORITY = 10;
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">
' . file_get_contents( __DIR__ . '/../../assets/js/notice.js' ) . '
</script>
'
);
$ajaxHandler->addScriptToAdminHead();
}
public function testProcessAjaxNoticeDismiss()
{
$_POST[AjaxHandler::POST_FIELD_NOTICE_NAME] = 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)
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment