From 3402c1e47c19c45ecf6aa991a986e398b5bbd1fe Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Mon, 16 Nov 2026 17:30:45 +0530 Subject: [PATCH 1/4] fix: attach cloud library listeners early --- assets/src/media/__tests__/admin-page.test.js | 38 +++++++++++++++++++ assets/src/media/admin-page.js | 14 ++++--- inc/dam.php | 4 +- tests/test-dam.php | 11 ++++++ 4 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 assets/src/media/__tests__/admin-page.test.js diff --git a/assets/src/media/__tests__/admin-page.test.js b/assets/src/media/__tests__/admin-page.test.js new file mode 100644 index 000000000..fa041c329 --- /dev/null +++ b/assets/src/media/__tests__/admin-page.test.js @@ -0,0 +1,38 @@ +/** + * Tests for the Cloud Library admin page script. + */ + +jest.mock( '../scss/admin-page.scss', () => ({}) ); + +const SITE_URL = 'https://example.test'; + +describe( 'Cloud Library admin page (issue #1211)', () => { + let iframe; + let loader; + + beforeAll( () => { + window.optmlAdminPage = { siteUrl: SITE_URL }; + + // The script is printed in the head, before the iframe markup exists. + require( '../admin-page.js' ); + + document.body.innerHTML = '
'; + iframe = document.getElementById( 'om-dam' ); + loader = document.querySelector( '.om-dam-loader' ); + }); + + test( 'reveals the iframe when it loads after the script ran', () => { + iframe.dispatchEvent( new Event( 'load' ) ); + + expect( loader.style.display ).toBe( 'none' ); + expect( iframe.style.display ).toBe( '' ); + }); + + test( 'answers getUrl from an iframe added after the script ran', () => { + const postMessage = jest.spyOn( iframe.contentWindow, 'postMessage' ).mockImplementation( () => {}); + + window.dispatchEvent( new MessageEvent( 'message', { data: { type: 'om-dam', action: 'getUrl' }}) ); + + expect( postMessage ).toHaveBeenCalledWith({ siteUrl: SITE_URL, type: 'om-dam', context: 'browse' }, '*' ); + }); +}); diff --git a/assets/src/media/admin-page.js b/assets/src/media/admin-page.js index f861a2fa1..2d7a6692b 100644 --- a/assets/src/media/admin-page.js +++ b/assets/src/media/admin-page.js @@ -3,12 +3,16 @@ import './scss/admin-page.scss'; const { optmlAdminPage } = window; const { siteUrl } = optmlAdminPage; -const iframe = document.getElementById( 'om-dam' ); -iframe.addEventListener( 'load', function() { +// This runs in the head, before #om-dam exists: `load` does not bubble, so catch it in the capture phase. +document.addEventListener( 'load', function( event ) { + if ( 'om-dam' !== event.target.id ) { + return; + } + document.querySelector( '.om-dam-loader' ).style.display = 'none'; - iframe.style.display = ''; -}); + event.target.style.display = ''; +}, true ); window.addEventListener( 'message', function( event ) { if ( ! event.data ) { @@ -31,5 +35,5 @@ window.addEventListener( 'message', function( event ) { return; } - iframe.contentWindow.postMessage({ siteUrl, type: 'om-dam', context: 'browse' }, '*' ); + document.getElementById( 'om-dam' ).contentWindow.postMessage({ siteUrl, type: 'om-dam', context: 'browse' }, '*' ); }); diff --git a/inc/dam.php b/inc/dam.php index 8bbeb4c99..5744e5a39 100644 --- a/inc/dam.php +++ b/inc/dam.php @@ -429,12 +429,12 @@ public function render_dashboard_page() { display: none !important; } -

...

+ assertEmpty( $data['sites'] ); } + public function test_admin_page_script_loads_in_head_issue_1211() { + set_current_screen( 'optimole_page_optimole-dam' ); + + $this->dam->enqueue_admin_page_scripts(); + + $this->assertTrue( wp_script_is( OPTML_NAMESPACE . '-admin-page', 'enqueued' ) ); + $this->assertFalse( wp_scripts()->get_data( OPTML_NAMESPACE . '-admin-page', 'group' ) ); + + set_current_screen( 'front' ); + } + public function test_insert_attachments() { foreach ( $this->inserted_ids as $index => $id ) { $this->assertIsInt( $id ); From 8f8b34c222dee5e05c8fbdff09bc25f241115a17 Mon Sep 17 00:00:00 2001 From: Girish Panchal <79647963+girishpanchal30@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:52:10 +0530 Subject: [PATCH 2/4] ci: build assets before PHPUnit tests --- .github/workflows/test-php.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/test-php.yml b/.github/workflows/test-php.yml index 134fd5f27..7c4c934bf 100644 --- a/.github/workflows/test-php.yml +++ b/.github/workflows/test-php.yml @@ -46,6 +46,14 @@ jobs: tools: phpunit-polyfills:1.1 - name: Checkout source code uses: actions/checkout@v4 + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 18 + - name: Build assets + run: | + npm ci + npm run build - name: Install WordPress Test Suite run: composer install-wp-tests - name: Install Composer dependencies @@ -58,6 +66,7 @@ jobs: steps: - name: Checkout source code uses: actions/checkout@v4 + - name: Setup PHP version - name: Setup PHP version uses: shivammathur/setup-php@v2 with: From 75b05104d57d31861b1ab3022028a0f27147928b Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Mon, 16 Nov 2026 17:54:56 +0530 Subject: [PATCH 3/4] fix: phpunit tests --- tests/test-dam.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/test-dam.php b/tests/test-dam.php index f3abaf804..94bbc17bf 100644 --- a/tests/test-dam.php +++ b/tests/test-dam.php @@ -136,15 +136,17 @@ public function test_iframe_url_all_cloud_sites_enabled() { $this->assertEmpty( $data['sites'] ); } - public function test_admin_page_script_loads_in_head_issue_1211() { - set_current_screen( 'optimole_page_optimole-dam' ); + public function test_dashboard_page_renders_loader_before_iframe_issue_1211() { + ob_start(); + $this->dam->render_dashboard_page(); + $html = ob_get_clean(); - $this->dam->enqueue_admin_page_scripts(); + $loader = strpos( $html, 'class="om-dam-loader"' ); + $iframe = strpos( $html, 'id="om-dam"' ); - $this->assertTrue( wp_script_is( OPTML_NAMESPACE . '-admin-page', 'enqueued' ) ); - $this->assertFalse( wp_scripts()->get_data( OPTML_NAMESPACE . '-admin-page', 'group' ) ); - - set_current_screen( 'front' ); + $this->assertNotFalse( $loader ); + $this->assertNotFalse( $iframe ); + $this->assertLessThan( $iframe, $loader ); } public function test_insert_attachments() { From b516245b9d46e5f797503e7ffad69afba37d6b41 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Mon, 16 Nov 2026 17:58:32 +0530 Subject: [PATCH 4/4] Revert "ci: build assets before PHPUnit tests" This reverts commit 8f8b34c222dee5e05c8fbdff09bc25f241115a17. --- .github/workflows/test-php.yml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/.github/workflows/test-php.yml b/.github/workflows/test-php.yml index 7c4c934bf..134fd5f27 100644 --- a/.github/workflows/test-php.yml +++ b/.github/workflows/test-php.yml @@ -46,14 +46,6 @@ jobs: tools: phpunit-polyfills:1.1 - name: Checkout source code uses: actions/checkout@v4 - - name: Setup Node - uses: actions/setup-node@v4 - with: - node-version: 18 - - name: Build assets - run: | - npm ci - npm run build - name: Install WordPress Test Suite run: composer install-wp-tests - name: Install Composer dependencies @@ -66,7 +58,6 @@ jobs: steps: - name: Checkout source code uses: actions/checkout@v4 - - name: Setup PHP version - name: Setup PHP version uses: shivammathur/setup-php@v2 with: