Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/php/Admin/Bootstrap_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,17 @@ public function get_active_notice( ?DateTimeImmutable $now = null ): ?array {
$now = $now ?? new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) );
$dismissed = get_user_meta( get_current_user_id(), self::DISMISSED_NOTICES_META_KEY, false );
$welcome = $this->welcome_client->get_banner();
$welcome_text = '' === ( $welcome['headline_free'] ?? '' ) ? ( $welcome['text_free'] ?? '' ) : $welcome['headline_free'];

if ( ! empty( $welcome['key'] ) && ! in_array( $welcome['key'], $dismissed, true ) &&
'' !== trim( wp_strip_all_tags( (string) $welcome_text ) ) &&
( empty( $welcome['start_datetime'] ) || $now >= $welcome['start_datetime'] ) &&
( empty( $welcome['end_datetime'] ) || $now <= $welcome['end_datetime'] ) ) {
return [
'key' => $welcome['key'],
'style' => self::PROMO_BANNER_STYLE,
'label' => $welcome['label'] ?? '',
'text' => '' === ( $welcome['headline_free'] ?? '' ) ? ( $welcome['text_free'] ?? '' ) : $welcome['headline_free'],
'text' => $welcome_text,
'secondary' => $welcome['secondary_free'] ?? '',
'action_url' => $welcome['action_url_free'] ?? '',
'action_label' => $welcome['action_label_free'] ?? '',
Expand Down
3 changes: 2 additions & 1 deletion src/php/Client/Welcome_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ function ( $section ) {
* }
*/
public function get_banner(): array {
return $this->welcome_data['banner'] ?? [];
$banner = $this->welcome_data['banner'] ?? [];
return is_array( $banner ) ? $banner : [];
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/Admin/Bootstrap_Admin_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,23 @@ public function test_dismissal_with_invalid_nonce_is_ignored(): void {
$this->assertEmpty( get_user_meta( self::get_user_id(), self::DISMISSED_META_KEY, false ) );
$this->assertIsArray( $admin->get_promo_banner( self::moment( self::DURING_BANNER ) ) );
}

/**
* A feed banner without any text is not shown, and the survey notice is offered instead.
*
* @return void
*/
public function test_feed_banner_without_text_is_not_shown(): void {
foreach ( [ '', ' ', '<strong> </strong>' ] as $text ) {
$admin = $this->make_admin(
[
'text_free' => $text,
'headline_free' => '',
]
);

$this->assertNull( $admin->get_promo_banner( self::moment( self::DURING_BANNER ) ), "Text: '$text'" );
$this->assertSame( 'survey', $admin->get_active_notice( self::moment( self::DURING_BANNER ) )['key'], "Text: '$text'" );
}
}
}
14 changes: 14 additions & 0 deletions tests/unit/Client/Welcome_Client_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,18 @@ public function test_banner_reads_promotion_fields_and_missing_dates(): void {
$this->assertSame( [ 'Only feature' ], array_column( $client->get_features(), 'title' ) );
$this->assertSame( [], $client->get_partners() );
}

/**
* Stored welcome data with a banner that is not a list is read as having no banner.
*
* @return void
*/
public function test_damaged_stored_banner_is_read_as_no_banner(): void {
set_transient( 'code_snippets_welcome_data', [ 'banner' => 'damaged' ] );

$banner = ( new Welcome_Client() )->get_banner();
delete_transient( 'code_snippets_welcome_data' );

$this->assertSame( [], $banner );
}
}
Loading