Skip to content

Commit

Permalink
Merge pull request #939 from carstingaxion/fix/unit-test-l10n-slugs
Browse files Browse the repository at this point in the history
Add & updated unit tests for localized slugs
  • Loading branch information
mauteri authored Oct 11, 2024
2 parents a0518fa + 0b18665 commit 4f685de
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 26 deletions.
4 changes: 2 additions & 2 deletions includes/core/classes/class-event-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ public function register_post_type(): void {
*/
public static function get_localized_post_type_slug(): string {
$switched_locale = switch_to_locale( get_locale() );
$slug = _x( 'event', 'Post Type Slug', 'gatherpress' );
$slug = sanitize_title( $slug, '', 'save' );
$slug = _x( 'Event', 'Post Type Singular Name', 'gatherpress' );
$slug = sanitize_title( $slug );
if ( $switched_locale ) {
restore_previous_locale();
}
Expand Down
8 changes: 4 additions & 4 deletions includes/core/classes/class-topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public function register_taxonomy(): void {
Event::POST_TYPE,
array(
'labels' => array(
'name' => _x( 'Topics', 'taxonomy general name', 'gatherpress' ),
'singular_name' => _x( 'Topic', 'taxonomy singular name', 'gatherpress' ),
'name' => _x( 'Topics', 'Taxonomy General Name', 'gatherpress' ),
'singular_name' => _x( 'Topic', 'Taxonomy Singular Name', 'gatherpress' ),
'search_items' => __( 'Search Topics', 'gatherpress' ),
'all_items' => __( 'All Topics', 'gatherpress' ),
'view_item' => __( 'View Topic', 'gatherpress' ),
Expand Down Expand Up @@ -123,8 +123,8 @@ public function register_taxonomy(): void {
*/
public static function get_localized_taxonomy_slug(): string {
$switched_locale = switch_to_locale( get_locale() );
$slug = _x( 'topic', 'Taxonomy Slug', 'gatherpress' );
$slug = sanitize_title( $slug, '', 'save' );
$slug = _x( 'Topic', 'Taxonomy Singular Name', 'gatherpress' );
$slug = sanitize_title( $slug );
if ( $switched_locale ) {
restore_previous_locale();
}
Expand Down
2 changes: 1 addition & 1 deletion includes/core/classes/class-venue.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function register_post_type(): void {
*/
public static function get_localized_post_type_slug(): string {
$switched_locale = switch_to_locale( get_locale() );
$slug = _x( 'venue', 'Post Type Slug', 'gatherpress' );
$slug = _x( 'Venue', 'Post Type Singular Name', 'gatherpress' );
$slug = sanitize_title( $slug );

if ( $switched_locale ) {
Expand Down
69 changes: 69 additions & 0 deletions test/unit/php/includes/core/classes/class-test-event-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,75 @@ public function test_register_post_type(): void {
$this->assertTrue( post_type_exists( Event::POST_TYPE ), 'Failed to assert that post type exists.' );
}

/**
* Coverage for get_localized_post_type_slug method.
*
* @covers ::get_localized_post_type_slug
*
* @return void
*/
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".'
);

$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 {
if ( 'Event' !== $text || 'Post Type Singular Name' !== $context ) {
return $translation;
}
return 'Ünit Tést';
};

/**
* Instead of loading additional languages into the unit test suite,
* we just filter the translated value, to mock different languages.
*
* Filters text with its translation based on context information for a domain.
*
* @param string $translation Translated text.
* @param string $text Text to translate.
* @param string $context Context information for the translators.
* @return string Translated text.
*/
add_filter( 'gettext_with_context_gatherpress', $filter, 10, 3 );

$this->assertSame(
'unit-test',
Event_Setup::get_localized_post_type_slug(),
'Failed to assert the post type slug is "unit-test".'
);

remove_filter( 'gettext_with_context_gatherpress', $filter );
}

/**
* Coverage for register_post_meta method.
*
Expand Down
22 changes: 9 additions & 13 deletions test/unit/php/includes/core/classes/class-test-rsvp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use GatherPress\Core\Event;
use GatherPress\Core\Rsvp;
use GatherPress\Core\Rsvp_Query;
use GatherPress\Core\Rsvp_Setup;
use PMC\Unit_Test\Base;
use WP_Comment_Query;

Expand Down Expand Up @@ -52,29 +51,26 @@ public function test_setup_hooks(): void {
/**
* Coverage for taxonomy_query method.
*
* @covers ::taxonomy_query
*
* @return void
*/
public function test_taxonomy_query(): void {
$instance = RSVP_Query::get_instance();
$instance = Rsvp_Query::get_instance();
$user_id = $this->factory->user->create();
$event = $this->mock->post( array( 'post_type' => Event::POST_TYPE ) )->get();
$post = $this->mock->post( array( 'post_type' => Event::POST_TYPE ) )->get();
$clauses = array(
'join' => '',
'where' => '',
);
$rsvp = wp_insert_comment(
array(
'comment_post_ID' => $event->ID,
'comment_type' => Rsvp::COMMENT_TYPE,
'user_id' => $user_id,
)
);
$event = new Event( $post->ID );

wp_set_object_terms( $rsvp, 'attending', Rsvp::TAXONOMY );
$event->rsvp->save( $user_id, 'attending' );

$comment_query = new WP_Comment_Query(
array(
'post_id' => $event->ID,
'user_id' => $user_id,
'tax_query' => array( //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
array(
'taxonomy' => Rsvp::TAXONOMY,
Expand All @@ -86,15 +82,15 @@ public function test_taxonomy_query(): void {
);

$pieces = $instance->taxonomy_query( $clauses, $comment_query );
$term = get_term_by( 'slug', 'attending', Rsvp::TAXONOMY );

$this->assertSame(
' LEFT JOIN wp_term_relationships ON (wp_comments.comment_ID = wp_term_relationships.object_id)',
$pieces['join'],
'Failed to assert that join is the same.'
);

$this->assertSame(
' AND ( wp_term_relationships.term_taxonomy_id IN (' . $user_id . ') )',
' AND ( wp_term_relationships.term_taxonomy_id IN (' . $term->term_id . ') )',
preg_replace( '/\s+/', ' ', $pieces['where'] ),
'Failed to assert where is the same.'
);
Expand Down
34 changes: 29 additions & 5 deletions test/unit/php/includes/core/classes/class-test-topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,40 @@ public function test_register_taxonomy(): void {
* @return void
*/
public function test_get_localized_taxonomy_slug(): void {
$instance = Topic::get_instance();

$this->assertSame(
'topic',
$instance->get_localized_taxonomy_slug(),
'Failed to assert english taxonomy slug is "topic".'
Topic::get_localized_taxonomy_slug(),
'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 {
if ( 'topic' !== $text || 'Taxonomy Slug' !== $context ) {
if ( 'Topic' !== $text || 'Taxonomy Singular Name' !== $context ) {
return $translation;
}
return 'Ünit Tést';
Expand All @@ -96,7 +120,7 @@ public function test_get_localized_taxonomy_slug(): void {

$this->assertSame(
'unit-test',
$instance->get_localized_taxonomy_slug(),
Topic::get_localized_taxonomy_slug(),
'Failed to assert taxonomy slug is "unit-test".'
);

Expand Down
55 changes: 54 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,8 +100,61 @@ public function test_get_localized_post_type_slug(): void {
$this->assertSame(
'venue',
Venue::get_localized_post_type_slug(),
'Failed to assert that post type slug is same.'
'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 {
if ( 'Venue' !== $text || 'Post Type Singular Name' !== $context ) {
return $translation;
}
return 'Ünit Tést';
};

/**
* Instead of loading additional languages into the unit test suite,
* we just filter the translated value, to mock different languages.
*
* Filters text with its translation based on context information for a domain.
*
* @param string $translation Translated text.
* @param string $text Text to translate.
* @param string $context Context information for the translators.
* @return string Translated text.
*/
add_filter( 'gettext_with_context_gatherpress', $filter, 10, 3 );

$this->assertSame(
'unit-test',
Venue::get_localized_post_type_slug(),
'Failed to assert the post type slug is "unit-test".'
);

remove_filter( 'gettext_with_context_gatherpress', $filter );
}

/**
Expand Down

0 comments on commit 4f685de

Please sign in to comment.