[ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 class GP_Test_Thing_Translation_set extends GP_UnitTestCase { 4 5 function test_copy_translations_from_should_copy_into_empty_set() { 6 $source_set = $this->factory->translation_set->create(); 7 $destination_set = $this->factory->translation_set->create(); 8 $translation = $this->factory->translation->create( array( 'translation_set_id' => $source_set->id ) ); 9 $destination_set->copy_translations_from( $source_set->id ); 10 $destination_set_translations = GP::$translation->find( array( 'translation_set_id' => $destination_set->id ) ); 11 12 $this->assertEquals( 1, count( $destination_set_translations ) ); 13 $this->assertEqualFields( $destination_set_translations[0], 14 array( 'translation_0' => $translation->translation_0, 'translation_set_id' => $destination_set->id, 'original_id' => $translation->original_id ) 15 ); 16 } 17 18 function test_import_should_save_user_info() { 19 $user = $this->factory->user->create( array( 'user_login' => 'pijo' ) ); 20 wp_set_current_user( $user ); 21 22 $set = $this->factory->translation_set->create_with_project_and_locale(); 23 $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string' ) ); 24 25 $translations_for_import = new Translations; 26 $translations_for_import->add_entry( array( 'singular' => 'A string','translations' => array( 'baba' ) ) ); 27 $set->import( $translations_for_import ); 28 29 $translations = GP::$translation->all(); 30 $this->assertEquals( $translations[0]->user_id, $user ); 31 } 32 33 function test_import_should_save_fuzzy() { 34 $set = $this->factory->translation_set->create_with_project_and_locale(); 35 $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string' ) ); 36 $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'Second string' ) ); 37 38 $translations_for_import = new Translations; 39 $translations_for_import->add_entry( array( 'singular' => 'A string', 'translations' => array( 'baba' ), 'flags' => array('fuzzy' ) ) ); 40 $translations_for_import->add_entry( array( 'singular' => 'Second string', 'translations' => array( 'second' ) ) ); 41 $set->import( $translations_for_import ); 42 43 $translations = GP::$translation->all(); 44 45 $this->assertEquals( $translations[0]->status, 'fuzzy' ); 46 $this->assertEquals( $translations[1]->status, 'current' ); 47 } 48 49 function test_import_should_save_forced_fuzzy() { 50 $set = $this->factory->translation_set->create_with_project_and_locale(); 51 $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string' ) ); 52 $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'Second string' ) ); 53 54 $fuzzy_import = new Translations; 55 $fuzzy_import->add_entry( array( 'singular' => 'A string', 'translations' => array( 'baba' ) ) ); 56 57 $current_import = new Translations; 58 $current_import->add_entry( array( 'singular' => 'Second string', 'translations' => array( 'second' ) ) ); 59 60 $set->import( $fuzzy_import, 'fuzzy' ); 61 $set->import( $current_import ); 62 63 $translations = GP::$translation->all(); 64 65 $this->assertEquals( $translations[0]->status, 'fuzzy' ); 66 $this->assertEquals( $translations[1]->status, 'current' ); 67 } 68 69 function test_import_should_skip_existing_when_importing_fuzzy() { 70 $set = $this->factory->translation_set->create_with_project_and_locale(); 71 $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string' ) ); 72 73 $fuzzy_import = new Translations; 74 $fuzzy_import->add_entry( array( 'singular' => 'A string', 'translations' => array( 'baba' ) ) ); 75 76 $translations_added = $set->import( $fuzzy_import, 'fuzzy' ); 77 $this->assertEquals( $translations_added, 1 ); 78 79 // Do the import again, it should not go through. 80 $translations_added = $set->import( $fuzzy_import, 'fuzzy' ); 81 $this->assertEquals( $translations_added, 0 ); 82 } 83 84 function test_import_should_generate_warnings() { 85 $set = $this->factory->translation_set->create_with_project_and_locale(); 86 $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string with %s' ) ); 87 88 $translations_for_import = new Translations; 89 $translations_for_import->add_entry( array( 'singular' => 'A string with %s', 'translations' => array( 'No Placeholder' ) ) ); 90 $set->import( $translations_for_import ); 91 92 $translations = GP::$translation->all(); 93 $this->assertArrayHasKey( 'placeholders', $translations[0]->warnings[0] ); 94 } 95 96 function test_import_should_not_import_translations_with_warnings_twice() { 97 $set = $this->factory->translation_set->create_with_project_and_locale(); 98 $original = $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string with %s' ) ); 99 100 $translations_for_import = new Translations; 101 $translations_for_import->add_entry( array( 'singular' => 'A string with %s', 'translations' => array( 'No Placeholder' ) ) ); 102 $translations_added = $set->import( $translations_for_import ); 103 $this->assertEquals( $translations_added, 1 ); 104 105 $translations = GP::$translation->all(); 106 $this->assertArrayHasKey( 'placeholders', $translations[0]->warnings[0] ); 107 108 // Do the import again, it should not go through. 109 $translations_added = $set->import( $translations_for_import ); 110 $this->assertEquals( $translations_added, 0 ); 111 } 112 113 /** 114 * @ticket gh-646 115 */ 116 function test_import_should_not_change_fuzzy_status_if_warnings() { 117 $set = $this->factory->translation_set->create_with_project_and_locale(); 118 $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string with %s' ) ); 119 120 $translations_for_import = new Translations; 121 $translations_for_import->add_entry( array( 'singular' => 'A string with %s', 'translations' => array( 'No Placeholder' ) ) ); 122 $set->import( $translations_for_import, 'fuzzy' ); 123 124 $translations = GP::$translation->all(); 125 $this->assertArrayHasKey( 'placeholders', $translations[0]->warnings[0] ); 126 $this->assertSame( 'fuzzy', $translations[0]->status ); 127 } 128 129 function test_import_should_not_import_existing_same_translation() { 130 $set = $this->factory->translation_set->create_with_project_and_locale(); 131 $original = $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string' ) ); 132 $translation = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original->id, 'translations' => array( 'baba' ), 'status' => 'current' ) ); 133 134 $translations_for_import = new Translations; 135 $translations_for_import->add_entry( array( 'singular' => 'A string', 'translations' => array( 'baba' ) ) ); 136 $translations_added = $set->import( $translations_for_import ); 137 138 $this->assertEquals( $translations_added, 0 ); 139 140 } 141 142 function test_import_should_import_over_existing_different_translation_by_default() { 143 $set = $this->factory->translation_set->create_with_project_and_locale(); 144 $original = $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string' ) ); 145 $translation = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original->id, 'translations' => array( 'baba' ), 'status' => 'current' ) ); 146 147 $translations_for_import = new Translations; 148 $translations_for_import->add_entry( array( 'singular' => 'A string', 'translations' => array( 'abab' ) ) ); 149 $translations_added = $set->import( $translations_for_import ); 150 151 $this->assertEquals( $translations_added, 1 ); 152 153 } 154 155 function test_filter_translation_set_import_over_existing() { 156 $set = $this->factory->translation_set->create_with_project_and_locale(); 157 $original = $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string' ) ); 158 $translation = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original->id, 'translations' => array( 'baba' ), 'status' => 'current' ) ); 159 160 $translations_for_import = new Translations; 161 $translations_for_import->add_entry( array( 'singular' => 'A string', 'translations' => array( 'abab' ) ) ); 162 163 add_filter( 'gp_translation_set_import_over_existing', '__return_false' ); 164 $translations_added = $set->import( $translations_for_import ); 165 remove_filter( 'gp_translation_set_import_over_existing', '__return_false' ); 166 167 $this->assertEquals( $translations_added, 0 ); 168 } 169 170 function test_user_can_reject_his_own_translation() { 171 $set = $this->factory->translation_set->create_with_project_and_locale(); 172 $original = $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string' ) ); 173 $translation = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original->id, 'translations' => array( 'baba' ), 'status' => 'current' ) ); 174 175 $this->assertFalse( $translation->set_status( 'current' ) ); 176 $this->assertTrue( $translation->set_status( 'rejected' ) ); 177 $this->assertTrue( $translation->set_status( 'waiting' ) ); 178 $this->assertFalse( $translation->set_status( 'current' ) ); 179 180 $translation = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original->id, 'translations' => array( 'baba2' ), 'status' => 'rejected' ) ); 181 182 $this->assertFalse( $translation->set_status( 'current' ) ); 183 $this->assertTrue( $translation->set_status( 'rejected' ) ); 184 $this->assertFalse( $translation->set_status( 'current' ) ); 185 186 $translation = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original->id, 'translations' => array( 'baba3' ), 'status' => 'waiting' ) ); 187 188 $this->assertFalse( $translation->set_status( 'current' ) ); 189 $this->assertTrue( $translation->set_status( 'rejected' ) ); 190 $this->assertFalse( $translation->set_status( 'current' ) ); 191 } 192 193 /** 194 * @ticket 512 195 */ 196 function test_filter_translation_set_import_fuzzy_translations() { 197 $set = $this->factory->translation_set->create_with_project_and_locale(); 198 199 $translations_for_import = new Translations; 200 201 // Create 3 originals and 3 fuzzy translations 202 for ( $i = 0; $i < 3; $i++ ) { 203 $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => "A string #$i" ) ); 204 $translations_for_import->add_entry( array( 205 'singular' => "A string #$i", 206 'translations' => array( "A translated string #$i" ), 207 'flags' => array( 'fuzzy' ) 208 ) ); 209 } 210 211 // Create 3 originals and 3 non-fuzzy translations 212 for ( $i = 0; $i < 3; $i++ ) { 213 $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => "A second string #$i" ) ); 214 $translations_for_import->add_entry( array( 215 'singular' => "A second string #$i", 216 'translations' => array( "A second translated string #$i" ), 217 ) ); 218 } 219 220 // Import 6 translations 221 add_filter( 'gp_translation_set_import_fuzzy_translations', '__return_false' ); 222 $translations_added = $set->import( $translations_for_import ); 223 remove_filter( 'gp_translation_set_import_fuzzy_translations', '__return_false' ); 224 225 // Expect only 3 imported translations, fuzzy translations are ignored. 226 $this->assertEquals( $translations_added, 3 ); 227 } 228 229 function test_delete() { 230 $set = $this->factory->translation_set->create_with_project_and_locale(); 231 232 $pre_delete = GP::$translation_set->find_one( array( 'id' => $set->id ) ); 233 234 $set->delete(); 235 236 $post_delete = GP::$translation_set->find_one( array( 'id' => $set->id ) ); 237 238 $this->assertFalse( empty( $pre_delete ) ); 239 $this->assertNotEquals( $pre_delete, $post_delete ); 240 } 241 242 /** 243 * @ticket gh-397 244 */ 245 function test_update_status_breakdown_updates_pre_2_1_cached_values() { 246 global $wpdb; 247 $set = $this->factory->translation_set->create_with_project_and_locale(); 248 249 $counts = array(); 250 $statuses = GP::$translation->get_static( 'statuses' ); 251 foreach ( $statuses as $status ) { 252 $counts[] = (object) array( 'translation_status' => $status, 'n' => 10 ); 253 } 254 $counts[] = (object) array( 'translation_status' => 'warnings', 'n' => 1 ); 255 wp_cache_set( $set->id, $counts, 'translation_set_status_breakdown' ); 256 257 $num_queries = $wpdb->num_queries; 258 $set->update_status_breakdown(); 259 $this->assertEquals( $num_queries + 8, $wpdb->num_queries ); 260 } 261 262 public function test_created_action_is_called() { 263 $action = new MockAction(); 264 265 add_action( 'gp_translation_set_created', array( $action, 'action' ) ); 266 267 $this->factory->translation_set->create_with_project_and_locale(); 268 269 remove_action( 'gp_translation_set_created', array( $action, 'action' ) ); 270 271 $this->assertSame( 1, $action->get_call_count() ); 272 } 273 274 public function test_saved_action_is_called() { 275 $action = new MockAction(); 276 277 $set = $this->factory->translation_set->create_with_project_and_locale(); 278 $locale = $this->factory->locale->create(); 279 280 add_action( 'gp_translation_set_saved', array( $action, 'action' ) ); 281 282 $set->save( array( 283 'locale' => $locale->slug, 284 ) ); 285 286 remove_action( 'gp_translation_set_saved', array( $action, 'action' ) ); 287 288 $this->assertSame( 1, $action->get_call_count() ); 289 } 290 291 public function test_deleted_action_is_called() { 292 $action = new MockAction(); 293 294 $set = $this->factory->translation_set->create_with_project_and_locale(); 295 296 add_action( 'gp_translation_set_deleted', array( $action, 'action' ) ); 297 298 $set->delete(); 299 300 remove_action( 'gp_translation_set_deleted', array( $action, 'action' ) ); 301 302 $this->assertSame( 1, $action->get_call_count() ); 303 } 304 305 public function test_previous_state_is_passed_to_saved_action() { 306 $translation_set = $this->factory->translation_set->create_with_project_and_locale( array( 'name' => 'Before' ) ); 307 $initial_set = clone $translation_set; 308 309 $previous_set = null; 310 $closure = function( $set_after, $set_before ) use ( &$previous_set ) { 311 $previous_set = $set_before; 312 }; 313 314 add_action( 'gp_translation_set_saved', $closure, 10, 2 ); 315 316 $translation_set->save( array( 'name' => 'After' ) ); 317 318 remove_action( 'gp_translation_set_saved', $closure ); 319 320 $this->assertEquals( $initial_set, $previous_set ); 321 $this->assertEquals( $previous_set->name, 'Before' ); 322 $this->assertEquals( $translation_set->name, 'After' ); 323 } 324 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:01:04 2024 | Cross-referenced by PHPXref 0.7.1 |