[ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 global $update_invocation_count; 4 $update_invocation_count = 0; 5 6 class MockOriginal extends GP_Original { 7 function update( $data, $where = null ) { 8 $GLOBALS['update_invocation_count']++; 9 return parent::update( $data, $where ); 10 } 11 } 12 13 class GP_Test_Thing_Original extends GP_UnitTestCase { 14 15 function create_original_with_update_counter( $original_args = array() ) { 16 $project = $this->factory->project->create(); 17 /* We are doing it this hackish way, because I could not make the PHPUnit mocker to count the update() invocations */ 18 $mock_original = new MockOriginal; 19 $this->factory->original = new GP_UnitTest_Factory_For_Original( $this->factory, $mock_original ); 20 $original = $this->factory->original->create( array_merge( array( 'project_id' => $project->id ), $original_args ) ); 21 // the object doesn't retrieve default values, we need to select it back from the database to get them 22 $original->reload(); 23 return array( $project, $original ); 24 } 25 26 function create_translations_with( $entries ) { 27 $translations = new Translations; 28 foreach( $entries as $entry ) { 29 $translations->add_entry( $entry ); 30 } 31 return $translations; 32 } 33 34 function test_import_for_project_should_not_update_unchanged_originals() { 35 list( $project, $original ) = $this->create_original_with_update_counter(); 36 $translations = $this->create_translations_with( array( array( 'singular' => $original->singular ) ) ); 37 $original->import_for_project( $project, $translations ); 38 $this->assertEquals( 0, $GLOBALS['update_invocation_count'], 'update should be invoked only 2 times' ); 39 } 40 41 function test_import_for_project_should_update_changed_originals() { 42 list( $project, $original ) = $this->create_original_with_update_counter( array( 43 'comment' => 'Some comment' 44 ) ); 45 $translations = $this->create_translations_with( array( array( 'singular' => $original->singular ) ) ); 46 $original->import_for_project( $project, $translations ); 47 $this->assertEquals( 1, $GLOBALS['update_invocation_count'], 'update should be invoked 3 times' ); 48 } 49 50 function test_import_for_project_should_update_cache() { 51 $project = $this->factory->project->create(); 52 $original = $this->factory->original->create( array( 'project_id' => $project->id, 'status' => '+active', 'singular' => 'baba' ) ); 53 54 $translations_array = array( array( 'singular' => $original->singular ), array( 'singular' => 'dyado' ) ); 55 $translations = $this->create_translations_with( $translations_array ); 56 $original->import_for_project( $project, $translations ); 57 58 $this->assertEquals( count( $translations_array ), $original->count_by_project_id( $project->id ) ); 59 } 60 61 function test_is_different_from_should_return_true_if_only_singular_is_for_update_and_it_is_the_same() { 62 $original = $this->factory->original->create(); 63 $this->assertFalse( GP::$original->is_different_from( array( 'singular' => $original->singular ), $original ) ); 64 } 65 66 function test_is_different_from_should_return_true_if_one_value_is_empty_string_and_the_other_is_null() { 67 $original = $this->factory->original->create( array( 'comment' => NULL ) ); 68 $this->assertFalse( GP::$original->is_different_from( array( 'singular' => $original->singular, 'comment' => '' ), $original ) ); 69 } 70 71 function test_is_different_from_should_use_this_if_second_argument_is_not_supplied() { 72 $original = $this->factory->original->create(); 73 $data = array( 'singular' => 'baba' ); 74 $this->assertEquals( GP::$original->is_different_from( $data, $original ), $original->is_different_from( $data ) ); 75 } 76 77 /** 78 * @ticket gh-301 79 */ 80 function test_original_should_validate_if_singular_is_zero() { 81 $project = $this->factory->project->create(); 82 $original = $this->factory->original->create( array( 'project_id' => $project->id, 'status' => '+active', 'singular' => '0', 'priority' => -1 ) ); 83 $this->assertTrue( $original->validate() ); 84 } 85 86 function test_import_should_leave_unchanged_strings_as_active() { 87 $project = $this->factory->project->create(); 88 $original = $this->factory->original->create( array( 'project_id' => $project->id, 'status' => '+active', 'singular' => 'baba' ) ); 89 $translations = $this->create_translations_with( array( array( 'singular' => 'baba' ) ) ); 90 $original->import_for_project( $project, $translations ); 91 $originals_for_project = $original->by_project_id( $project->id ); 92 $this->assertEquals( 1, count( $originals_for_project ) ); 93 $this->assertEquals( 'baba', $originals_for_project[0]->singular ); 94 } 95 96 function test_import_should_mark_translation_of_changed_strings_as_fuzzy() { 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' => 'baba baba' ) ); 99 $translation = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original->id, 'status' => 'current' ) ); 100 $translations_for_import = $this->create_translations_with( array( array( 'singular' => 'baba baba.' ) ) ); 101 102 list( $originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted, $originals_error ) = $original->import_for_project( $set->project, $translations_for_import ); 103 104 $this->assertEquals( 0, $originals_added ); 105 $this->assertEquals( 0, $originals_existing ); 106 $this->assertEquals( 1, $originals_fuzzied ); 107 $this->assertEquals( 0, $originals_obsoleted ); 108 $this->assertEquals( 0, $originals_error ); 109 110 $current_translations = GP::$translation->find_many( "original_id = '{$original->id}' AND status = 'current'" ); 111 $fuzzy_translations = GP::$translation->find_many( "original_id = '{$original->id}' AND status = 'fuzzy'" ); 112 113 $this->assertEquals( 0, count( $current_translations ) ); 114 $this->assertEquals( 1, count( $fuzzy_translations ) ); 115 } 116 117 function test_import_should_not_mark_translation_of_changed_strings_as_fuzzy_with_filter() { 118 $set = $this->factory->translation_set->create_with_project_and_locale(); 119 $original = $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => 'baba baba' ) ); 120 $translation = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original->id, 'status' => 'current' ) ); 121 $translations_for_import = $this->create_translations_with( array( array( 'singular' => 'baba baba.' ) ) ); 122 123 add_filter( 'gp_set_translations_for_original_to_fuzzy', '__return_false' ); 124 125 list( $originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted, $originals_error ) = $original->import_for_project( $set->project, $translations_for_import ); 126 127 remove_filter( 'gp_set_translations_for_original_to_fuzzy', '__return_false' ); 128 129 $this->assertEquals( 0, $originals_added ); 130 $this->assertEquals( 1, $originals_existing ); 131 $this->assertEquals( 0, $originals_fuzzied ); 132 $this->assertEquals( 0, $originals_obsoleted ); 133 $this->assertEquals( 0, $originals_error ); 134 135 $current_translations = GP::$translation->find_many( "original_id = '{$original->id}' AND status = 'current'" ); 136 $fuzzy_translations = GP::$translation->find_many( "original_id = '{$original->id}' AND status = 'fuzzy'" ); 137 138 $this->assertEquals( 1, count( $current_translations ) ); 139 $this->assertEquals( 0, count( $fuzzy_translations ) ); 140 } 141 142 /** 143 * @ticket 508 144 */ 145 function test_import_should_clean_project_count_cache() { 146 $set = $this->factory->translation_set->create_with_project_and_locale(); 147 $original = $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+obsolete', 'singular' => 'baba baba' ) ); 148 149 $count = $original->count_by_project_id( $set->project->id ); 150 $this->assertEquals( 0, $count ); 151 152 $translations_for_import = $this->create_translations_with( array( array( 'singular' => 'baba baba' ) ) ); 153 154 list( $originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted, $originals_error ) = $original->import_for_project( $set->project, $translations_for_import ); 155 156 $this->assertEquals( 0, $originals_added ); 157 $this->assertEquals( 1, $originals_existing ); 158 $this->assertEquals( 0, $originals_fuzzied ); 159 $this->assertEquals( 0, $originals_obsoleted ); 160 $this->assertEquals( 0, $originals_error ); 161 162 $count = $original->count_by_project_id( $set->project->id ); 163 $this->assertEquals( 1, $count ); 164 } 165 166 function test_import_should_remove_from_active_missing_strings() { 167 $project = $this->factory->project->create(); 168 $original = $this->factory->original->create( array( 'project_id' => $project->id, 'status' => '+active' ) ); 169 $original->import_for_project( $project, new Translations ); 170 $originals_for_project = $original->by_project_id( $project->id ); 171 $this->assertEquals( 0, count( $originals_for_project ) ); 172 } 173 174 function test_normalize_fields_should_convert_named_priorities_to_numeric_by_name() { 175 $original = new GP_Original; 176 $normalized_args = $original->normalize_fields( array( 'priority' => 'hidden' ) ); 177 $this->assertEquals( -2, $normalized_args['priority'] ); 178 } 179 180 function test_normalize_fields_should_not_convert_numeric_priorities_to_numeric_by_name() { 181 $original = new GP_Original; 182 $normalized_args = $original->normalize_fields( array( 'priority' => '1' ) ); 183 $this->assertEquals( 1, $normalized_args['priority'] ); 184 } 185 186 function test_normalize_fields_should_unset_priority_if_named_priority_is_missing() { 187 $original = new GP_Original; 188 $normalized_args = $original->normalize_fields( array( 'priority' => 'baba' ) ); 189 $this->assertFalse( isset( $normalized_args['priority'] ) ); 190 } 191 192 function test_by_project_id_and_entry_should_match_case() { 193 $project = $this->factory->project->create(); 194 $original = $this->factory->original->create( array( 'project_id' => $project->id, 'status' => '+active', 'singular' => 'Baba' ) ); 195 196 $entry = new stdClass(); 197 $entry->singular = 'BABA'; 198 199 $by_project_id_and_entry = GP::$original->by_project_id_and_entry( $project->id, $entry ); 200 $this->assertEquals( false, $by_project_id_and_entry ); 201 202 $entry->singular = 'Baba'; 203 $by_project_id_and_entry = GP::$original->by_project_id_and_entry( $project->id, $entry ); 204 $this->assertSame( $original->singular, $by_project_id_and_entry->singular ); 205 } 206 207 /** 208 * @ticket gh-302 209 */ 210 function test_import_for_project_with_context_which_exceeds_the_maximum_length_of_255() { 211 $project = $this->factory->project->create(); 212 213 // Insert an original with a context with 255 chars. It shouldn't be removed. 214 $this->factory->original->create( array( 'project_id' => $project->id, 'status' => '+active', 'singular' => 'foo', 'context' => str_repeat( 'a', 255 ) ) ); 215 216 /* 217 * Create three originals. 218 * 1) Same as existing original, but with 256 chars. 219 * 2) New original wit 256 chars. 220 * 3) New original with 255 chars. 221 */ 222 $test_originals = array( 223 array( 'singular' => 'foo', 'context' => str_repeat( 'a', 256 ) ), 224 array( 'singular' => 'bar', 'context' => str_repeat( 'b', 256 ) ), 225 array( 'singular' => 'bab', 'context' => str_repeat( 'c', 255 ) ), 226 ); 227 228 // First import: 229 230 $translations_for_import = $this->create_translations_with( $test_originals ); 231 list( $originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted, $originals_error ) = GP::$original->import_for_project( $project, $translations_for_import ); 232 233 // Only two new originals. 234 $this->assertEquals( 2, $originals_added ); 235 $this->assertEquals( 0, $originals_existing ); 236 $this->assertEquals( 0, $originals_fuzzied ); 237 $this->assertEquals( 0, $originals_obsoleted ); 238 $this->assertEquals( 0, $originals_error ); 239 240 $originals = GP::$original->by_project_id( $project->id ); 241 $this->assertCount( 3, $originals ); 242 243 // Second import: 244 245 $translations_for_import = $this->create_translations_with( $test_originals ); 246 list( $originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted, $originals_error ) = GP::$original->import_for_project( $project, $translations_for_import ); 247 248 // Originals are already imported, no change. 249 $this->assertEquals( 0, $originals_added ); 250 $this->assertEquals( 0, $originals_existing ); 251 $this->assertEquals( 0, $originals_fuzzied ); 252 $this->assertEquals( 0, $originals_obsoleted ); 253 $this->assertEquals( 0, $originals_error ); 254 255 $originals = GP::$original->by_project_id( $project->id ); 256 $this->assertCount( 3, $originals ); 257 258 // Get the first item. 259 $original = reset( $originals ); 260 $this->assertSame( str_repeat( 'a', 255 ), $original->context ); 261 } 262 263 /** 264 * @ticket gh-397 265 */ 266 function test_return_types_of_count_by_project_id() { 267 $project = $this->factory->project->create(); 268 269 $this->factory->original->create( array( 'project_id' => $project->id, 'status' => '+active', 'singular' => 'Foo' ) ); 270 $this->factory->original->create( array( 'project_id' => $project->id, 'status' => '+active', 'singular' => 'Bar', 'priority' => -2 ) ); 271 272 $total = GP::$original->count_by_project_id( $project->id ); 273 $this->assertSame( 2, $total ); 274 275 $all = GP::$original->count_by_project_id( $project->id, 'all' ); 276 $this->assertInternalType( 'object', $all ); 277 $this->assertSame( 2, $all->total ); 278 $this->assertSame( 1, $all->hidden ); 279 $this->assertSame( 1, $all->public ); 280 281 $hidden = GP::$original->count_by_project_id( $project->id, 'hidden' ); 282 $this->assertSame( 1, $hidden ); 283 284 $public = GP::$original->count_by_project_id( $project->id, 'public' ); 285 $this->assertSame( 1, $public ); 286 287 $non_existent = GP::$original->count_by_project_id( $project->id, 'non_existent' ); 288 $this->assertSame( 0, $non_existent ); 289 } 290 291 public function test_previous_state_is_passed_to_saved_action() { 292 $project = $this->factory->project->create(); 293 $original = $this->factory->original->create( array( 'project_id' => $project->id, 'status' => '+active', 'singular' => 'Before' ) ); 294 $initial_original = clone $original; 295 296 $previous_original = null; 297 $closure = function( $original_after, $original_before ) use ( &$previous_original ) { 298 $previous_original = $original_before; 299 }; 300 301 add_action( 'gp_original_saved', $closure, 10, 2 ); 302 303 $original->save( array( 'singular' => 'After' ) ); 304 305 remove_action( 'gp_original_saved', $closure ); 306 307 $this->assertEquals( $initial_original, $previous_original ); 308 $this->assertEquals( $previous_original->singular, 'Before' ); 309 $this->assertEquals( $original->singular, 'After' ); 310 } 311 312 public function test_delete() { 313 $set = $this->factory->translation_set->create_with_project_and_locale(); 314 $translation = $this->factory->translation->create_with_original_for_translation_set( $set ); 315 316 $original = GP::$original->get( $translation->original_id ); 317 $this->assertSame( 1, $original->delete() ); 318 319 // Check if translations are deleted too. 320 $translation = GP::$translation->find_one( array( 'id' => $translation->id ) ); 321 $this->assertFalse( $translation ); 322 } 323 324 /** 325 * @ticket gh-1340 326 */ 327 function test_import_should_respect_priority_in_flags() { 328 $project = $this->factory->project->create(); 329 $original = $this->factory->original->create( array( 'project_id' => $project->id, 'status' => '+active', 'singular' => 'baba' ) ); 330 331 $translations = $this->create_translations_with( 332 array( 333 array( 'singular' => 'baba', 'flags' => array( 'gp-priority: low' ) ), 334 array( 'singular' => 'baba baba' ), 335 array( 'singular' => 'baba baba baba', 'flags' => array( 'gp-priority: high' ) ), 336 array( 'singular' => 'priority flag should be ignored.', 'flags' => array( 'gp-priority: unexpected' ) ), 337 ) 338 ); 339 340 $original->import_for_project( $project, $translations ); 341 342 $originals_for_project = $original->by_project_id( $project->id ); 343 $this->assertEquals( 4, count( $originals_for_project ) ); 344 345 $this->assertEquals( -1, $originals_for_project[0]->priority, 'Existing string should have been updated to be low-priority.' ); 346 $this->assertEquals( 0, $originals_for_project[1]->priority, 'New string should have imported as normal priority.' ); 347 $this->assertEquals( 1, $originals_for_project[2]->priority, 'New string should have imported as high priority.' ); 348 $this->assertEquals( 0, $originals_for_project[3]->priority, 'New string with invalid priority flag should have imported as normal priority.' ); 349 } 350 351 }
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 |