Skip to content

Commit

Permalink
Add tests with different user_locale set
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Oct 5, 2024
1 parent 0580545 commit 704bbc7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
25 changes: 24 additions & 1 deletion test/unit/php/includes/core/classes/class-test-event-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,32 @@ public function test_get_localized_post_type_slug(): void {
$this->assertSame(
'event',
Event_Setup::get_localized_post_type_slug(),
'Failed to assert english post type slug is "event".'
'Failed to assert English post type slug is "event".'
);

$user_id = $this->factory->user->create();
update_user_meta( $user_id, 'locale', 'es_ES' );
switch_to_user_locale( $user_id );

// @todo This assertion CAN NOT FAIL,
// until real translations do exist in the wp-env instance.
// Because WordPress doesn't have any translation files to load,
// it will return the string in English.
$this->assertSame(
'event',
Event_Setup::get_localized_post_type_slug(),
'Failed to assert post type slug is "event", even the locale is not English anymore.'
);
// But at least the restoring of the user locale can be tested, without .po files.
$this->assertSame(
'es_ES',
determine_locale(),
'Failed to assert locale was reset to Spanish, after switching to ~ and restoring from English.'
);

// Restore default locale for following tests.
switch_to_locale( 'en_US' );

// This also checks that the post type is still registered with the same 'Post Type Singular Name' label,
// which is used by the method under test and the test itself.
$filter = static function ( string $translation, string $text, string $context ): string {
Expand Down
25 changes: 24 additions & 1 deletion test/unit/php/includes/core/classes/class-test-topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,32 @@ public function test_get_localized_taxonomy_slug(): void {
$this->assertSame(
'topic',
Topic::get_localized_taxonomy_slug(),
'Failed to assert english taxonomy slug is "topic".'
'Failed to assert English taxonomy slug is "topic".'
);

$user_id = $this->factory->user->create();
update_user_meta( $user_id, 'locale', 'es_ES' );
switch_to_user_locale( $user_id );

// @todo This assertion CAN NOT FAIL,
// until real translations do exist in the wp-env instance.
// Because WordPress doesn't have any translation files to load,
// it will return the string in English.
$this->assertSame(
'topic',
Topic::get_localized_taxonomy_slug(),
'Failed to assert taxonomy slug is "topic", even the locale is not English anymore.'
);
// But at least the restoring of the user locale can be tested, without .po files.
$this->assertSame(
'es_ES',
determine_locale(),
'Failed to assert locale was reset to Spanish, after switching to ~ and restoring from English.'
);

// Restore default locale for following tests.
switch_to_locale( 'en_US' );

// This also checks that the taxonomy is still registered with the same 'Taxonomy Singular Name' label,
// which is used by the method under test and the test itself.
$filter = static function ( string $translation, string $text, string $context ): string {
Expand Down
25 changes: 24 additions & 1 deletion test/unit/php/includes/core/classes/class-test-venue.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,32 @@ public function test_get_localized_post_type_slug(): void {
$this->assertSame(
'venue',
Venue::get_localized_post_type_slug(),
'Failed to assert english post type slug is "venue".'
'Failed to assert English post type slug is "venue".'
);

$user_id = $this->factory->user->create();
update_user_meta( $user_id, 'locale', 'es_ES' );
switch_to_user_locale( $user_id );

// @todo This assertion CAN NOT FAIL,
// until real translations do exist in the wp-env instance.
// Because WordPress doesn't have any translation files to load,
// it will return the string in English.
$this->assertSame(
'venue',
Venue::get_localized_post_type_slug(),
'Failed to assert post type slug is "venue", even the locale is not English anymore.'
);
// But at least the restoring of the user locale can be tested, without .po files.
$this->assertSame(
'es_ES',
determine_locale(),
'Failed to assert locale was reset to Spanish, after switching to ~ and restoring from English.'
);

// Restore default locale for following tests.
switch_to_locale( 'en_US' );

// This also checks that the post type is still registered with the same 'Post Type Singular Name' label,
// which is used by the method under test and the test itself.
$filter = static function ( string $translation, string $text, string $context ): string {
Expand Down

0 comments on commit 704bbc7

Please sign in to comment.