Make WordPress Core


Ignore:
Timestamp:
02/12/2024 12:58:53 PM (4 months ago)
Author:
gziolo
Message:

Script Modules API: Add deregister module function

It was impossible to deregister a script module. It is changing to avoid problems for extenders that want to override any Core script module.

Fixes #60463.
Props cbravobernal, gziolo, mukesh27, youknowriad.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/script-modules/wpScriptModules.php

    r57492 r57593  
    127127    }
    128128
     129
     130    /**
     131     * Tests that a script module can be deregistered
     132     * after being enqueued, and that will be removed
     133     * from the enqueue list too.
     134     *
     135     * @ticket 60463
     136     *
     137     * @covers ::register()
     138     * @covers ::enqueue()
     139     * @covers ::deregister()
     140     * @covers ::get_enqueued_script_modules()
     141     */
     142    public function test_wp_deregister_script_module() {
     143        $this->script_modules->register( 'foo', '/foo.js' );
     144        $this->script_modules->register( 'bar', '/bar.js' );
     145        $this->script_modules->enqueue( 'foo' );
     146        $this->script_modules->enqueue( 'bar' );
     147        $this->script_modules->deregister( 'foo' ); // Dequeued.
     148
     149        $enqueued_script_modules = $this->get_enqueued_script_modules();
     150
     151        $this->assertCount( 1, $enqueued_script_modules );
     152        $this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
     153        $this->assertTrue( isset( $enqueued_script_modules['bar'] ) );
     154    }
     155
     156    /**
     157     * Tests that a script module is not deregistered
     158     * if it has not been registered before, causing
     159     * no errors.
     160     *
     161     * @ticket 60463
     162     *
     163     * @covers ::deregister()
     164     * @covers ::get_enqueued_script_modules()
     165     */
     166    public function test_wp_deregister_unexistent_script_module() {
     167        $this->script_modules->deregister( 'unexistent' );
     168        $enqueued_script_modules = $this->get_enqueued_script_modules();
     169
     170        $this->assertCount( 0, $enqueued_script_modules );
     171        $this->assertFalse( isset( $enqueued_script_modules['unexistent'] ) );
     172    }
     173
     174    /**
     175     * Tests that a script module is not deregistered
     176     * if it has been deregistered previously, causing
     177     * no errors.
     178     *
     179     * @ticket 60463
     180     *
     181     * @covers ::get_enqueued_script_modules()
     182     * @covers ::register()
     183     * @covers ::deregister()
     184     * @covers ::enqueue()
     185     */
     186    public function test_wp_deregister_already_deregistered_script_module() {
     187        $this->script_modules->register( 'foo', '/foo.js' );
     188        $this->script_modules->enqueue( 'foo' );
     189        $this->script_modules->deregister( 'foo' ); // Dequeued.
     190        $enqueued_script_modules = $this->get_enqueued_script_modules();
     191
     192        $this->assertCount( 0, $enqueued_script_modules );
     193        $this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
     194
     195        $this->script_modules->deregister( 'foo' ); // Dequeued.
     196        $enqueued_script_modules = $this->get_enqueued_script_modules();
     197
     198        $this->assertCount( 0, $enqueued_script_modules );
     199        $this->assertFalse( isset( $enqueued_script_modules['foo'] ) );
     200    }
     201
    129202    /**
    130203    * Tests that a script module can be enqueued before it is registered, and will
Note: See TracChangeset for help on using the changeset viewer.