-
Notifications
You must be signed in to change notification settings - Fork 14
fix(campaigns): show above-header prompts without JS delay (NPPM-2934) #449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
298458b
61c2c12
4f45953
04f4350
11523f4
b972577
72411a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php // phpcs:disable WordPress.Files.FileName.InvalidClassFileName | ||
| /** | ||
| * Newspack_Popups_Model stub for tests. | ||
| * | ||
| * The Perfmatters integration calls | ||
| * \Newspack_Popups_Model::has_published_above_header_prompts() guarded by | ||
| * method_exists(). newspack-popups is not loaded in this test suite, so this stub | ||
| * stands in for it and its return value is toggled via | ||
| * `Newspack_Popups_Model::$has_above_header`. | ||
| * | ||
| * @package Newspack\Tests | ||
| */ | ||
|
|
||
| if ( ! class_exists( 'Newspack_Popups_Model' ) ) { | ||
| /** | ||
| * Minimal Newspack_Popups_Model stub. Only the surface the integration touches. | ||
| */ | ||
| class Newspack_Popups_Model { | ||
| /** | ||
| * Stubbed detection result. | ||
| * | ||
| * @var bool | ||
| */ | ||
| public static $has_above_header = false; | ||
|
|
||
| /** | ||
| * Stubbed detection method. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public static function has_published_above_header_prompts() { | ||
| return self::$has_above_header; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,127 @@ | ||
| <?php | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Severity: suggestion GitHub reports this PR as
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed the diagnosis: NPPM-193 (commit 5aa59b0) is on origin/main but not in this branch, so neither |
||
| /** | ||
| * Tests for the Perfmatters integration WooCommerce veto. | ||
| * Tests the Perfmatters integration (NPPM-2934). | ||
| * | ||
| * @package Newspack | ||
| * @package Newspack\Tests | ||
| */ | ||
|
|
||
| use Newspack\Perfmatters; | ||
| use Newspack\WooCommerce_Content_Detector; | ||
|
|
||
| require_once __DIR__ . '/../mocks/newspack-popups-model-mock.php'; | ||
|
|
||
| /** | ||
| * Test the perfmatters_disable_woocommerce_scripts callback. | ||
| * Tests the Perfmatters integration: above-header prompt handling and the | ||
| * WooCommerce veto. | ||
| */ | ||
| class Newspack_Test_Perfmatters extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Reset the detector memo before each test. | ||
| */ | ||
| public function setUp(): void { | ||
| parent::setUp(); | ||
| public function set_up() { | ||
| parent::set_up(); | ||
| WooCommerce_Content_Detector::reset_memo(); | ||
| } | ||
|
|
||
| /** | ||
| * Reset the detector memo after each test. | ||
| * Reset the above-header flag and the detector memo after each test. | ||
| */ | ||
| public function tearDown(): void { | ||
| public function tear_down() { | ||
| \Newspack_Popups_Model::$has_above_header = false; | ||
| WooCommerce_Content_Detector::reset_memo(); | ||
| parent::tearDown(); | ||
| parent::tear_down(); | ||
| } | ||
|
|
||
| /** | ||
| * Without above-header prompts, the prompt reveal scripts stay in the JS delay queue. | ||
| */ | ||
| public function test_reveal_scripts_delayed_without_above_header_prompts() { | ||
| \Newspack_Popups_Model::$has_above_header = false; | ||
|
|
||
| $options = Perfmatters::set_defaults( [] ); | ||
|
|
||
| $this->assertContains( 'newspack-popups', $options['assets']['delay_js_inclusions'] ); | ||
| $this->assertContains( 'window.newspack', $options['assets']['delay_js_inclusions'] ); | ||
| $this->assertNotContains( 'newspack-popups', $options['assets']['js_exclusions'] ); | ||
| $this->assertNotContains( 'newspack-plugin', $options['assets']['js_exclusions'] ); | ||
| } | ||
|
|
||
| /** | ||
| * With published above-header prompts, the reveal scripts are removed from the JS | ||
| * delay queue and excluded from deferral so the prompts appear immediately. | ||
| */ | ||
| public function test_reveal_scripts_undelayed_with_above_header_prompts() { | ||
| \Newspack_Popups_Model::$has_above_header = true; | ||
|
|
||
| $options = Perfmatters::set_defaults( [] ); | ||
|
|
||
| $this->assertNotContains( 'newspack-popups', $options['assets']['delay_js_inclusions'] ); | ||
| $this->assertNotContains( 'window.newspack', $options['assets']['delay_js_inclusions'] ); | ||
| $this->assertNotContains( 'newspack-plugin', $options['assets']['delay_js_inclusions'] ); | ||
|
|
||
| $this->assertContains( 'newspack-popups', $options['assets']['js_exclusions'] ); | ||
| $this->assertContains( 'newspack-plugin', $options['assets']['js_exclusions'] ); | ||
|
adekbadek marked this conversation as resolved.
|
||
|
|
||
| // `window.newspack` is an inline token; deferral only applies to external <script src> | ||
| // files, so it is intentionally kept out of the defer exclusions even while present in | ||
| // the delay exclusions. Assert the asymmetry so the two lists cannot silently drift. | ||
| $this->assertNotContains( 'window.newspack', $options['assets']['js_exclusions'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Perfmatters persists its delay list whenever its settings are saved through the UI, | ||
| * so on a configured site the stored option already contains the reveal scripts. They | ||
| * must be subtracted from the merged list, not merely omitted from Newspack's own | ||
| * contribution – otherwise the merge puts them back and the prompts stay delayed on | ||
| * exactly the sites this targets. | ||
| */ | ||
| public function test_reveal_scripts_undelayed_when_already_in_saved_option() { | ||
| \Newspack_Popups_Model::$has_above_header = true; | ||
|
|
||
| $options = Perfmatters::set_defaults( | ||
| [ | ||
| 'assets' => [ | ||
| 'delay_js_inclusions' => [ 'newspack-popups', 'window.newspack', 'newspack-plugin', 'publisher-script' ], | ||
| ], | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertNotContains( 'newspack-popups', $options['assets']['delay_js_inclusions'] ); | ||
| $this->assertNotContains( 'window.newspack', $options['assets']['delay_js_inclusions'] ); | ||
| $this->assertNotContains( 'newspack-plugin', $options['assets']['delay_js_inclusions'] ); | ||
|
|
||
| // The publisher's own entries survive. | ||
| $this->assertContains( 'publisher-script', $options['assets']['delay_js_inclusions'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Without above-header prompts, a saved delay list keeps the reveal scripts. | ||
| */ | ||
| public function test_saved_delay_list_is_preserved_without_above_header_prompts() { | ||
| \Newspack_Popups_Model::$has_above_header = false; | ||
|
|
||
| $options = Perfmatters::set_defaults( | ||
| [ | ||
| 'assets' => [ | ||
| 'delay_js_inclusions' => [ 'newspack-popups', 'publisher-script' ], | ||
| ], | ||
| ] | ||
| ); | ||
|
|
||
| $this->assertContains( 'newspack-popups', $options['assets']['delay_js_inclusions'] ); | ||
| $this->assertContains( 'publisher-script', $options['assets']['delay_js_inclusions'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Unrelated scripts remain delayed regardless of above-header prompts. | ||
| */ | ||
| public function test_other_scripts_still_delayed_with_above_header_prompts() { | ||
| \Newspack_Popups_Model::$has_above_header = true; | ||
|
|
||
| $options = Perfmatters::set_defaults( [] ); | ||
|
|
||
| $this->assertContains( 'newspack-blocks', $options['assets']['delay_js_inclusions'] ); | ||
| $this->assertContains( 'recaptcha', $options['assets']['delay_js_inclusions'] ); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.