From 81090bc2e24d1f8b9b502b5bf2429efde5816d1b Mon Sep 17 00:00:00 2001 From: Simon Wheatley Date: Fri, 7 Aug 2015 13:09:26 +0100 Subject: [PATCH 1/4] Add tests for creating empty translations These should pass when translations are correctly falling back to content in the canonical language. #219 --- class-post-public.php | 3 +++ tests/test-translations.php | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/class-post-public.php b/class-post-public.php index 889afac..fdbfd52 100644 --- a/class-post-public.php +++ b/class-post-public.php @@ -1298,6 +1298,9 @@ public function get_shadow_post_types( $base_post_type ) { * Returns the post in a particular language, or the fallback content * if there's no post available. * + * @FIXME: Currently this function is broken by Babble_Jobs::create_empty_translation + * See: test_canonical_content_fallback + * * @param int|WP_Post $post Either a WP Post object, or a post ID * @param string $lang_code The language code for the required language * @param boolean $fallback If true: if a post is not available, fallback to the default language content (defaults to true) diff --git a/tests/test-translations.php b/tests/test-translations.php index 4a8f28f..b60e5e8 100644 --- a/tests/test-translations.php +++ b/tests/test-translations.php @@ -133,4 +133,16 @@ public function test_term_translations() { } + public function test_canonical_content_fallback() { + $this->assertSame( 'en_US', get_locale() ); + + $en = $this->factory->post->create_and_get(); + $fr = bbl_get_post_in_lang( $en->ID, 'fr_FR', true ); + + // @FIXME: These tests fail due to the interaction of Babble_Post_Public::get_post_in_lang and Babble_Jobs::create_empty_translation + $this->assertSame( $en->post_title, $fr->post_title ); + $this->assertSame( $en->post_content, $fr->post_content ); + + } + } From ec989860ede2a8138ea1bdd045e6ee877a5b9f1c Mon Sep 17 00:00:00 2001 From: Simon Wheatley Date: Fri, 7 Aug 2015 13:10:06 +0100 Subject: [PATCH 2/4] Change filter names to make consistent with the rest of Babble All filters and actions should now start with `bbl_` --- class-jobs.php | 8 ++++---- translation-show-pre-translation.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/class-jobs.php b/class-jobs.php index f0d6691..35fb58c 100755 --- a/class-jobs.php +++ b/class-jobs.php @@ -29,7 +29,7 @@ public function __construct() { add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); add_action( 'add_meta_boxes_bbl_job', array( $this, 'add_meta_boxes_bbl_job' ), 999 ); add_action( 'admin_init', array( $this, 'admin_init' ) ); - add_action( 'babble_create_empty_translation', array( $this, 'create_empty_translation' ) ); + add_action( 'bbl_cron_create_empty_translations', array( $this, 'create_empty_translation' ) ); add_action( 'bbl_translation_post_meta_boxes', array( $this, 'bbl_translation_post_meta_boxes' ), 10, 3 ); add_action( 'bbl_translation_submit_meta_boxes', array( $this, 'bbl_translation_submit_meta_boxes' ), 10, 2 ); add_action( 'bbl_translation_terms_meta_boxes', array( $this, 'bbl_translation_terms_meta_boxes' ), 10, 2 ); @@ -1081,7 +1081,7 @@ public function get_job_objects( $job ) { } /** - * Create empty translations of a post for all languages. Called via WP-Cron on the `babble_create_empty_translation` hook. + * Create empty translations of a post for all languages. Called via WP-Cron on the `bbl_cron_create_empty_translations` hook. * * @param array $args Args array containing a `post_id` element. */ @@ -1130,9 +1130,9 @@ public function create_post_jobs( $post_id, array $lang_codes ) { if ( bbl_get_default_lang_code() == $lang_code ) continue; - if ( apply_filters( 'bbl_create_empty_translation', false, $post ) ) { + if ( apply_filters( 'bbl_trigger_create_empty_translations', false, $post ) ) { - wp_schedule_single_event( time(), 'babble_create_empty_translation', array( + wp_schedule_single_event( time(), 'bbl_cron_create_empty_translations', array( array( 'post_id' => $post->ID, ) diff --git a/translation-show-pre-translation.php b/translation-show-pre-translation.php index 99c337f..ea8a62e 100644 --- a/translation-show-pre-translation.php +++ b/translation-show-pre-translation.php @@ -26,4 +26,4 @@ */ -add_action( 'bbl_create_empty_translation', '__return_true' ); \ No newline at end of file +add_action( 'bbl_trigger_create_empty_translations', '__return_true' ); \ No newline at end of file From be65a9c884ed80a15803fb1d99388e00b0a451d1 Mon Sep 17 00:00:00 2001 From: Simon Wheatley Date: Fri, 7 Aug 2015 15:23:40 +0100 Subject: [PATCH 3/4] Change method name to `create_empty_translations` from `create_empty_translation` --- class-jobs.php | 4 ++-- class-post-public.php | 2 +- tests/test-translations.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/class-jobs.php b/class-jobs.php index 35fb58c..af997c7 100755 --- a/class-jobs.php +++ b/class-jobs.php @@ -29,7 +29,7 @@ public function __construct() { add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); add_action( 'add_meta_boxes_bbl_job', array( $this, 'add_meta_boxes_bbl_job' ), 999 ); add_action( 'admin_init', array( $this, 'admin_init' ) ); - add_action( 'bbl_cron_create_empty_translations', array( $this, 'create_empty_translation' ) ); + add_action( 'bbl_cron_create_empty_translations', array( $this, 'create_empty_translations' ) ); add_action( 'bbl_translation_post_meta_boxes', array( $this, 'bbl_translation_post_meta_boxes' ), 10, 3 ); add_action( 'bbl_translation_submit_meta_boxes', array( $this, 'bbl_translation_submit_meta_boxes' ), 10, 2 ); add_action( 'bbl_translation_terms_meta_boxes', array( $this, 'bbl_translation_terms_meta_boxes' ), 10, 2 ); @@ -1085,7 +1085,7 @@ public function get_job_objects( $job ) { * * @param array $args Args array containing a `post_id` element. */ - public function create_empty_translation( array $args ) { + public function create_empty_translations( array $args ) { global $bbl_post_public; if ( !$post = get_post( $args['post_id'] ) ) { diff --git a/class-post-public.php b/class-post-public.php index fdbfd52..0c2cb4f 100644 --- a/class-post-public.php +++ b/class-post-public.php @@ -1298,7 +1298,7 @@ public function get_shadow_post_types( $base_post_type ) { * Returns the post in a particular language, or the fallback content * if there's no post available. * - * @FIXME: Currently this function is broken by Babble_Jobs::create_empty_translation + * @FIXME: Currently this function is broken by Babble_Jobs::create_empty_translations * See: test_canonical_content_fallback * * @param int|WP_Post $post Either a WP Post object, or a post ID diff --git a/tests/test-translations.php b/tests/test-translations.php index b60e5e8..ae4275b 100644 --- a/tests/test-translations.php +++ b/tests/test-translations.php @@ -139,7 +139,7 @@ public function test_canonical_content_fallback() { $en = $this->factory->post->create_and_get(); $fr = bbl_get_post_in_lang( $en->ID, 'fr_FR', true ); - // @FIXME: These tests fail due to the interaction of Babble_Post_Public::get_post_in_lang and Babble_Jobs::create_empty_translation + // @FIXME: These tests fail due to the interaction of Babble_Post_Public::get_post_in_lang and Babble_Jobs::create_empty_translations $this->assertSame( $en->post_title, $fr->post_title ); $this->assertSame( $en->post_content, $fr->post_content ); From 946e81bf300a0a16edee8445f56d7793e0c7b472 Mon Sep 17 00:00:00 2001 From: Simon Wheatley Date: Thu, 13 Aug 2015 21:57:50 +0100 Subject: [PATCH 4/4] Call the method to create empty translations during the test --- tests/test-translations.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test-translations.php b/tests/test-translations.php index ae4275b..8cb5342 100644 --- a/tests/test-translations.php +++ b/tests/test-translations.php @@ -137,6 +137,8 @@ public function test_canonical_content_fallback() { $this->assertSame( 'en_US', get_locale() ); $en = $this->factory->post->create_and_get(); + // In normal operation, the create_empty_translations method is called on an immediate single cron job + $GLOBALS['bbl_jobs']->create_empty_translations($en->ID); $fr = bbl_get_post_in_lang( $en->ID, 'fr_FR', true ); // @FIXME: These tests fail due to the interaction of Babble_Post_Public::get_post_in_lang and Babble_Jobs::create_empty_translations