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/admin.php b/inc/admin.php index 0641d2599..4438d099a 100755 --- a/inc/admin.php +++ b/inc/admin.php @@ -717,7 +717,7 @@ public function add_notice_upgrade() { ?>

- @@ -772,6 +772,8 @@ public function should_show_upgrade() { /** * Build upgrade URL with current user's encoded email. * + * The URL is already localized, so callers must not pass it through tsdk_translate_link() again. + * * @return string Upgrade URL. */ public static function get_upgrade_base_link() { @@ -1741,7 +1743,7 @@ public function get_bf_notices( $user_plan ) { '' . $promo_code . '', '' . __( '25% discount', 'optimole-wp' ) . '' ), - 'cta_link' => esc_url_raw( tsdk_utmify( tsdk_translate_link( self::get_upgrade_base_link() ), 'bfcm', 'sidebarnotice' ) ), + 'cta_link' => esc_url_raw( tsdk_utmify( self::get_upgrade_base_link(), 'bfcm', 'sidebarnotice' ) ), ]; // The banner shares the SDK dismissal, which resets every year. @@ -1914,7 +1916,7 @@ private function get_dashboard_strings() { 'notice_disabled_account' => sprintf( /* translators: 1 anchor tag to pricing, 2 is the ending endin anchor tag, 3 is the bold tag start, 4 ending bold tag, 5 new line tag */ __( '%3$sYour account has been disabled due to exceeding quota.%4$s All images are being redirected to the original unoptimized URL. %5$sPlease %1$supgrade%2$s to re-activate the account.', 'optimole-wp' ), - '', + '', '', '', '', 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_dashboard_page_renders_loader_before_iframe_issue_1211() { + ob_start(); + $this->dam->render_dashboard_page(); + $html = ob_get_clean(); + + $loader = strpos( $html, 'class="om-dam-loader"' ); + $iframe = strpos( $html, 'id="om-dam"' ); + + $this->assertNotFalse( $loader ); + $this->assertNotFalse( $iframe ); + $this->assertLessThan( $iframe, $loader ); + } + public function test_insert_attachments() { foreach ( $this->inserted_ids as $index => $id ) { $this->assertIsInt( $id ); diff --git a/tests/test-upgrade-link.php b/tests/test-upgrade-link.php new file mode 100644 index 000000000..f713f59ae --- /dev/null +++ b/tests/test-upgrade-link.php @@ -0,0 +1,229 @@ +initial_service_data = $settings->get( 'service_data' ); + + $settings->update( + 'service_data', + [ + 'cdn_key' => 'example', + 'cdn_secret' => 'test', + 'whitelist' => [ 'example.org' ], + 'user_email' => self::ACCOUNT_EMAIL, + 'plan' => 'free', + 'visitors_limit' => 2000, + 'visitors_left' => 100, + ] + ); + } + + /** + * Restore the settings, which outlive the database rollback because they are cached statically. + */ + public function tearDown(): void { + ( new Optml_Settings() )->update( 'service_data', $this->initial_service_data ); + + remove_all_filters( 'themeisle_sdk_is_black_friday_sale' ); + remove_all_filters( 'themeisle_sdk_current_date' ); + set_current_screen( 'front' ); + + parent::tearDown(); + } + + /** + * Locales for which the SDK adds a language code to a link, with that code. + * + * @return array + */ + public function localized_locales(): array { + return [ + 'de_DE' => [ 'de_DE', 'de' ], + 'de_DE_formal' => [ 'de_DE_formal', 'de' ], + 'es_ES' => [ 'es_ES', 'es' ], + 'fr_FR' => [ 'fr_FR', 'fr' ], + 'it_IT' => [ 'it_IT', 'it' ], + 'ja' => [ 'ja', 'ja' ], + 'nl_NL' => [ 'nl_NL', 'nl' ], + 'nl_NL_formal' => [ 'nl_NL_formal', 'nl' ], + 'ro_RO' => [ 'ro_RO', 'ro' ], + ]; + } + + /** + * Log in an administrator who uses the given locale, and check how the SDK localizes links for them. + * + * @param string $locale User locale. + * @param string $code Language code the SDK uses for that locale, empty when it uses none. + */ + private function login_admin_with_locale( string $locale = 'de_DE', string $code = 'de' ): void { + wp_set_current_user( + self::factory()->user->create( + [ + 'role' => 'administrator', + 'locale' => $locale, + ] + ) + ); + + $this->assertSame( $locale, get_user_locale() ); + $this->assertSame( + '' === $code ? 'https://example.org/page/' : "https://example.org/$code/page/", + tsdk_translate_link( 'https://example.org/page/' ) + ); + } + + /** + * Assert a link is the upgrade page with the language code once, carrying the encoded account email. + * + * @param string $link Link to check. + * @param string $code Expected language code, empty for none. + * @param string $message Failure message. + */ + private function assert_upgrade_link( string $link, string $code = 'de', string $message = '' ): void { + $path = '' === $code ? '/upgrade' : "/$code/upgrade"; + + $this->assertStringStartsWith( "https://optimole.com$path?email=", $link, $message ); + + parse_str( (string) wp_parse_url( $link, PHP_URL_QUERY ), $query ); + $this->assertSame( self::ACCOUNT_EMAIL, base64_decode( $query['email'] ), $message ); + } + + /** + * Get the optimole.com links of some markup. + * + * @param string $html Markup. + * + * @return string[] + */ + private function get_optimole_links( string $html ): array { + preg_match_all( '~href="(https://optimole\.com/[^"]*)"~', $html, $matches ); + + return array_map( 'html_entity_decode', $matches[1] ); + } + + /** + * The upgrade link has the language code of the user once, and keeps the account email. + * + * @dataProvider localized_locales + * + * @param string $locale User locale. + * @param string $code Language code the SDK uses for that locale. + */ + public function test_upgrade_link_has_the_language_code_once( string $locale, string $code ): void { + $this->login_admin_with_locale( $locale, $code ); + + $this->assert_upgrade_link( Optml_Admin::get_upgrade_base_link(), $code ); + } + + /** + * A locale the SDK does not localize gets the upgrade link without a language code. + */ + public function test_upgrade_link_has_no_language_code_for_other_locales(): void { + $this->login_admin_with_locale( 'en_US', '' ); + + $this->assert_upgrade_link( Optml_Admin::get_upgrade_base_link(), '' ); + } + + /** + * The Media Library upgrade notice does not add the language code a second time. + */ + public function test_upgrade_notice_link_has_the_language_code_once(): void { + $this->login_admin_with_locale(); + set_current_screen( 'upload' ); + + ob_start(); + ( new Optml_Admin() )->add_notice_upgrade(); + $output = (string) ob_get_clean(); + + $links = $this->get_optimole_links( $output ); + + $this->assertCount( 1, $links ); + $this->assert_upgrade_link( $links[0] ); + } + + /** + * Neither Black Friday notice adds the language code a second time. + */ + public function test_black_friday_links_have_the_language_code_once(): void { + $this->login_admin_with_locale(); + add_filter( 'themeisle_sdk_is_black_friday_sale', '__return_true' ); + add_filter( + 'themeisle_sdk_current_date', + static function () { + return new DateTime( 'last Friday of November ' . gmdate( 'Y' ) ); + } + ); + + $notices = ( new Optml_Admin() )->get_bf_notices( 'free' ); + + foreach ( [ 'sidebar', 'banner' ] as $notice ) { + $this->assertArrayHasKey( $notice, $notices ); + $this->assert_upgrade_link( $notices[ $notice ]['cta_link'], 'de', "Wrong upgrade link in the $notice notice." ); + } + } + + /** + * Nothing the dashboard app receives links to optimole.com with two language codes. + */ + public function test_dashboard_data_has_no_doubled_language_code(): void { + $this->login_admin_with_locale(); + + $localize = new ReflectionMethod( Optml_Admin::class, 'localize_dashboard_app' ); + $localize->setAccessible( true ); + $data = $localize->invoke( new Optml_Admin() ); + + $this->assert_upgrade_link( $data['upgrade_url'] ); + + $disabled_account_links = $this->get_optimole_links( $data['strings']['notice_disabled_account'] ); + $this->assertCount( 1, $disabled_account_links ); + $this->assert_upgrade_link( $disabled_account_links[0] ); + + array_walk_recursive( + $data, + function ( $value, $key ) { + if ( is_string( $value ) ) { + $this->assertDoesNotMatchRegularExpression( self::DOUBLED_LANGUAGE_PATTERN, $value, "Doubled language code in '$key'." ); + } + } + ); + } +}