Skip to content
Snippets Groups Projects
Select Git revision
  • f7d2e9f4caf8ffb8ff5dac6630c1257df10077f2
  • master default protected
  • bugfix/wordpress-review
  • fix/duplicate
  • bugfix/get_current_screen_fail
  • feature/dismiss-nonce
  • replace-dodgy-path
  • bugfix/notice-not-show
  • devel
  • 3.3.0
  • 3.2.5
  • 3.2.4
  • 3.2.3
  • 3.2.2
  • 3.2.1
  • 3.2.0
  • 3.2.0-beta7
  • 3.2.0-beta6
  • 3.2.0-beta5
  • 3.2.0-beta4
  • 3.2.0-beta3
  • 3.2.0-beta2
  • 3.2.0-beta1
  • 3.1.4
  • 3.1.4-beta1
  • 3.1.3
  • 3.1.1
  • 3.1
  • 3.0
29 results

notice.js

Blame
  • TestAjaxHandler.php 2.93 KiB
    <?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()
        {
            $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">
        jQuery(document).on("click",".notice-dismiss",function(){var a=jQuery(this).closest("div.notice").data("notice-name");var b=jQuery(this).closest("div.notice").data("source");if(""!==a){jQuery.ajax({url:ajaxurl,type:"post",data:{action:"wpdesk_notice_dismiss",notice_name:a,source:b},success:function(c){}})}});jQuery(document).on("click",".notice-dismiss-link",function(){jQuery(this).closest("div.notice").data("source",jQuery(this).data("source"));jQuery(this).closest("div.notice").find(".notice-dismiss").click()});</script>
    '
            );
    
            $ajaxHandler->addScriptToAdminHead();
        }