[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/tests/phpunit/testcases/tests_things/ -> test_thing_translation.php (source)

   1  <?php
   2  
   3  class GP_Test_Thing_Translation extends GP_UnitTestCase {
   4  
   5  	function test_translation_approve_change_status() {
   6          $object_type = GP::$validator_permission->object_type;
   7          $user = $this->factory->user->create();
   8          wp_set_current_user( $user );
   9  
  10          $set = $this->factory->translation_set->create_with_project_and_locale();
  11          $permission = array( 'user_id' => $user, 'action' => 'approve',
  12                               'project_id' => $set->project_id, 'locale_slug' => $set->locale, 'set_slug' => $set->slug );
  13          GP::$validator_permission->create( $permission );
  14  
  15          $translation = $this->factory->translation->create_with_original_for_translation_set( $set );
  16  
  17          // Put the current count already in the cache
  18          $set->current_count();
  19  
  20          $translation->set_status('current');
  21          $set->update_status_breakdown(); // Refresh the counts of the object but not the cache
  22  
  23          $for_translation = GP::$translation->for_translation( $set->project, $set, 0, array( 'status' => 'current' ) );
  24  
  25          $this->assertEquals( 1, count( $for_translation ) );
  26          $this->assertEquals( 1, $set->current_count() );
  27      }
  28  
  29  	function test_translation_denied_approve_change_status() {
  30          $object_type = GP::$validator_permission->object_type;
  31          $user = $this->factory->user->create();
  32          wp_set_current_user( $user );
  33  
  34          $set = $this->factory->translation_set->create_with_project_and_locale();
  35          $permission = array( 'user_id' => $user, 'action' => 'approve',
  36                               'project_id' => $set->project_id, 'locale_slug' => $set->locale, 'set_slug' => $set->slug );
  37          GP::$validator_permission->create( $permission );
  38  
  39          $cannot_approve_translation = function( $verdict, $args ) {
  40              return 'approve' !== $args['action'] || 'translation' !== $args['object_type'];
  41          };
  42  
  43          add_filter( 'gp_pre_can_user', $cannot_approve_translation, 2, 2 );
  44          $translation = $this->factory->translation->create_with_original_for_translation_set( $set );
  45  
  46          // Put the current count already in the cache
  47          $set->current_count();
  48  
  49          $this->assertFalse( $translation->set_status( 'current' ) );
  50          $set->update_status_breakdown(); // Refresh the counts of the object but not the cache
  51          remove_filter( 'gp_pre_can_user', $cannot_approve_translation );
  52  
  53          $current_translations = GP::$translation->for_translation( $set->project, $set, 0, array( 'status' => 'current' ) );
  54          $waiting_translations = GP::$translation->for_translation( $set->project, $set, 0, array( 'status' => 'waiting' ) );
  55  
  56          $this->assertEquals( 0, count( $current_translations ) );
  57          $this->assertEquals( 0, $set->current_count() );
  58          $this->assertEquals( 1, count( $waiting_translations ) );
  59          $this->assertEquals( 1, $set->waiting_count() );
  60      }
  61  
  62  	function test_translation_should_support_6_plurals() {
  63          $plurals = array( 'translation_0' => 'Zero', 'translation_1' => 'One', 'translation_2' => 'Two', 'translation_3' => 'Three', 'translation_4' => 'Four', 'translation_5' => 'Five' );
  64          $translation = $this->factory->translation->create( $plurals );
  65  
  66          $this->assertEqualFields( $translation, $plurals );
  67      }
  68  
  69      function test_translation_should_write_all_6_plurals_to_database() {
  70          $plurals = array( 'translation_0' => 'Zero', 'translation_1' => 'One', 'translation_2' => 'Two', 'translation_3' => 'Three', 'translation_4' => 'Four', 'translation_5' => 'Five' );
  71          $translation = $this->factory->translation->create( $plurals );
  72          $translation->reload();
  73  
  74          $this->assertEqualFields( $translation, $plurals );
  75      }
  76  
  77      /**
  78       * @ticket 149
  79       * @ticket gh-236
  80       *
  81       * @covers GP_Translation::restrict_fields
  82       */
  83      function test_translation_should_not_validate_with_empty_plurals() {
  84          $data = array(
  85              'user_id'            => 1,
  86              'original_id'        => 1,
  87              'translation_set_id' => 1,
  88              'status'             => 'current',
  89          );
  90          $plurals = array(
  91              'translation_0' => 'Zero',
  92              'translation_1' => '',
  93              'translation_2' => '',
  94              'translation_3' => '',
  95              'translation_4' => '',
  96              'translation_5' => '',
  97          );
  98  
  99          $data = array_merge( $data, $plurals );
 100  
 101          $translation = $this->factory->translation->create( $data );
 102          $this->assertFalse( $translation->validate() );
 103          $this->assertCount( 5, $translation->errors );
 104      }
 105  
 106      /**
 107       * @ticket gh-341
 108       */
 109      function test_translation_should_not_report_empty_translation_set_id_as_translation_value_error() {
 110          $data = array(
 111              'user_id'            => 1,
 112              'original_id'        => 1,
 113              'status'             => 'current',
 114          );
 115          $plurals = array(
 116              'translation_0' => 'Zero',
 117              'translation_1' => '',
 118              'translation_2' => '',
 119              'translation_3' => '',
 120              'translation_4' => '',
 121              'translation_5' => '',
 122          );
 123  
 124          $data = array_merge( $data, $plurals );
 125  
 126          $translation = $this->factory->translation->create( $data );
 127          $this->assertFalse( $translation->validate() );
 128          $this->assertNotEquals( 'The textarea <strong>Translation 1</strong> is invalid and should be positive int!', $translation->errors[0] );
 129      }
 130  
 131      function test_for_translation_shouldnt_exclude_originals_with_rejected_translation_if_status_has_untranslated() {
 132          $object_type = GP::$validator_permission->object_type;
 133          $user = $this->factory->user->create();
 134          wp_set_current_user( $user );
 135  
 136          $set = $this->factory->translation_set->create_with_project_and_locale();
 137          $permission = array( 'user_id' => $user, 'action' => 'approve',
 138                               'project_id' => $set->project_id, 'locale_slug' => $set->locale, 'set_slug' => $set->slug );
 139          GP::$validator_permission->create( $permission );
 140          $translation = $this->factory->translation->create_with_original_for_translation_set( $set );
 141          $translation->reject();
 142          $for_translation = GP::$translation->for_translation( $set->project, $set, 0, array( 'status' => 'untranslated' ) );
 143  
 144          $this->assertEquals( 1, count( $for_translation ) );
 145          $this->assertEquals( null, $for_translation[0]->id );
 146      }
 147  
 148      function test_for_translation_should_include_untranslated_by_default() {
 149          $set = $this->factory->translation_set->create_with_project_and_locale();
 150  
 151          $original1 = $this->factory->original->create( array( 'project_id' => $set->project_id ) );
 152          $this->factory->original->create( array( 'project_id' => $set->project_id ) );
 153  
 154          $translation1 = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original1->id, 'status' => 'current' ) );
 155          $for_translation = GP::$translation->for_translation( $set->project, $set, 0, array(), array('by' => 'translation', 'how' => 'asc') );
 156  
 157          $this->assertEquals( 2, count( $for_translation ) );
 158          $this->assertEquals( null, $for_translation[0]->id );
 159          $this->assertEquals( $translation1->id, $for_translation[1]->id );
 160      }
 161  
 162      function test_for_translation_should_not_include_old_by_default() {
 163          $set = $this->factory->translation_set->create_with_project_and_locale();
 164  
 165          $original1 = $this->factory->original->create( array( 'project_id' => $set->project_id ) );
 166          $original2 = $this->factory->original->create( array( 'project_id' => $set->project_id ) );
 167  
 168          $translation1_old = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original1->id, 'status' => 'current' ) );
 169          $translation1_current = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original1->id, 'status' => 'current' ) );
 170          $translation1_current->set_as_current(); //$translation1_old is now old
 171  
 172          $for_translation = GP::$translation->for_translation( $set->project, $set, 0, array(), array('by' => 'translation', 'how' => 'asc') );
 173  
 174          $this->assertEquals( 2, count( $for_translation ) );
 175          $this->assertEquals( null, $for_translation[0]->id );
 176          $this->assertEquals( $translation1_current->id, $for_translation[1]->id );
 177      }
 178  
 179  
 180      function test_for_translation_should_not_include_untranslated_for_single_status() {
 181          $set = $this->factory->translation_set->create_with_project_and_locale();
 182  
 183          $original1 = $this->factory->original->create( array( 'project_id' => $set->project_id ) );
 184          $original2 = $this->factory->original->create( array( 'project_id' => $set->project_id ) ); //This isn't going to be translated
 185  
 186          $translation1 = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original1->id, 'status' => 'current' ) );
 187          $for_translation = GP::$translation->for_translation( $set->project, $set, 0, array('status' => 'current'), array('by' => 'translation', 'how' => 'asc') );
 188  
 189          $this->assertEquals( 1, count( $for_translation ) );
 190          $this->assertEquals( $translation1->id, $for_translation[0]->id );
 191      }
 192  
 193      function test_for_translation_should_respect_priorities() {
 194          $set = $this->factory->translation_set->create_with_project_and_locale();
 195  
 196          $original1 = $this->factory->original->create( array( 'project_id' => $set->project_id ) );
 197          $original2 = $this->factory->original->create( array( 'project_id' => $set->project_id, 'priority' => 1 ) );
 198  
 199          $for_translation = GP::$translation->for_translation( $set->project, $set, 0, array( 'status' => 'untranslated' ) );
 200          $this->assertEquals( 2, count( $for_translation ) );
 201  
 202          $for_translation = GP::$translation->for_translation( $set->project, $set, 0, array( 'status' => 'untranslated', 'priority' => array( 1 ) ) );
 203          $this->assertEquals( 1, count( $for_translation ) );
 204      }
 205  
 206  	function test_for_export_should_include_untranslated() {
 207          $set = $this->factory->translation_set->create_with_project_and_locale();
 208  
 209          $original1 = $this->factory->original->create( array( 'project_id' => $set->project_id ) );
 210          $original2 = $this->factory->original->create( array( 'project_id' => $set->project_id ) );
 211  
 212          $translation1 = $this->factory->translation->create( array( 'translation_set_id' => $set->id, 'original_id' => $original1->id, 'status' => 'current' ) );
 213          $for_export = GP::$translation->for_export( $set->project, $set, array( 'status' => 'current_or_untranslated' ) );
 214  
 215          $this->assertEquals( 2, count( $for_export ) );
 216  
 217          // We can't be sure which order the for_export call returned the strings, so make sure to compare the right one.
 218          if ( (int) $for_export[0]->original_id === (int) $translation1->original_id ) {
 219              $this->assertEquals( $translation1->id, $for_export[0]->id );
 220          } else {
 221              $this->assertEquals( $translation1->id, $for_export[1]->id );
 222          }
 223      }
 224  
 225  	function test_delete() {
 226          $set = $this->factory->translation_set->create_with_project_and_locale();
 227          $translation = $this->factory->translation->create_with_original_for_translation_set( $set );
 228  
 229          $pre_delete = GP::$translation->find_one( array( 'id' => $translation->id ) );
 230  
 231          $translation->delete();
 232  
 233          $post_delete = GP::$translation->find_one( array( 'id' => $translation->id ) );
 234  
 235          $this->assertFalse( empty( $pre_delete ) );
 236          $this->assertNotEquals( $pre_delete, $post_delete );
 237      }
 238  
 239      function test_validator_id_saved_on_status_change_to_current() {
 240          $set = $this->factory->translation_set->create_with_project_and_locale();
 241          $translation = $this->factory->translation->create_with_original_for_translation_set( $set );
 242          $translation->set_status('waiting');
 243  
 244          $user = $this->factory->user->create();
 245          wp_set_current_user( $user );
 246  
 247          GP::$validator_permission->create( array(
 248              'user_id' => $user, 'action' => 'approve',
 249              'project_id' => $set->project_id, 'locale_slug' => $set->locale,
 250              'set_slug' => $set->slug,
 251          ) );
 252  
 253          $translation->set_as_current();
 254          $this->assertEquals( $user, $translation->user_id_last_modified );
 255      }
 256  
 257      function test_validator_id_saved_on_status_change_to_rejected() {
 258          $set = $this->factory->translation_set->create_with_project_and_locale();
 259          $translation = $this->factory->translation->create_with_original_for_translation_set( $set );
 260          $translation->set_status( 'waiting' );
 261  
 262          $user = $this->factory->user->create();
 263          wp_set_current_user( $user );
 264  
 265          GP::$validator_permission->create( array(
 266              'user_id' => $user, 'action' => 'approve',
 267              'project_id' => $set->project_id, 'locale_slug' => $set->locale,
 268              'set_slug' => $set->slug,
 269          ) );
 270  
 271          $translation->set_status( 'rejected' );
 272          $this->assertEquals( $user, $translation->user_id_last_modified );
 273      }
 274  
 275      function test_cannot_reject_translation_without_approve_permission() {
 276          $set = $this->factory->translation_set->create_with_project_and_locale();
 277          $translation = $this->factory->translation->create_with_original_for_translation_set( $set );
 278          $this->assertTrue( $translation->set_status( 'waiting' ) );
 279  
 280          $user = $this->factory->user->create();
 281          wp_set_current_user( $user );
 282  
 283          $this->assertFalse( $translation->set_status( 'rejected' ) );
 284          $this->assertNotEquals( $user, $translation->user_id_last_modified );
 285      }
 286  
 287      function test_cannot_approve_translation_without_approve_permission() {
 288          $set = $this->factory->translation_set->create_with_project_and_locale();
 289          $translation = $this->factory->translation->create_with_original_for_translation_set( $set );
 290          $this->assertTrue( $translation->set_status( 'waiting' ) );
 291  
 292          $user = $this->factory->user->create();
 293          wp_set_current_user( $user );
 294  
 295          $this->assertFalse( $translation->set_status( 'current' ) );
 296          $this->assertNotEquals( $user, $translation->user_id_last_modified );
 297      }
 298  
 299  	public function test_previous_state_is_passed_to_saved_action() {
 300          $set = $this->factory->translation_set->create_with_project_and_locale();
 301          $translation = $this->factory->translation->create_with_original_for_translation_set( $set, array( 'translation_0' => 'Before' ) );
 302          $initial_translation = clone $translation;
 303  
 304          $previous_translation = null;
 305          $closure = function( $translation_after, $translation_before ) use ( &$previous_translation ) {
 306              $previous_translation = $translation_before;
 307          };
 308  
 309          add_action( 'gp_translation_saved', $closure, 10, 2 );
 310  
 311          $translation->save( array( 'translation_0' => 'After' ) );
 312  
 313          remove_action( 'gp_translation_saved', $closure );
 314  
 315          $this->assertEquals( $initial_translation, $previous_translation );
 316          $this->assertEquals( $previous_translation->translation_0, 'Before' );
 317          $this->assertEquals( $translation->translation_0, 'After' );
 318      }
 319  
 320      /**
 321       * @ticket gh-664
 322       */
 323  	function test_filter_by_permission() {
 324          $set = $this->factory->translation_set->create_with_project_and_locale();
 325          $translation = $this->factory->translation->create_with_original_for_translation_set( $set );
 326  
 327          // A new original has the priority 0.
 328          $this->assertEquals( 1, count( GP::$translation->for_translation( $set->project, $set, 0 ) ) );
 329          $this->assertEquals( 1, count( GP::$translation->for_translation( $set->project, $set, 0, array( 'priority' => array( '0' )  ) ) ) );
 330  
 331          // Invalid priority is the same as specifying no priority.
 332          $this->assertEquals( 1, count( GP::$translation->for_translation( $set->project, $set, 0, array( 'priority' => array( '10' )  ) ) ) );
 333  
 334          // String and numeric values should work.
 335          $this->assertEquals( 0, count( GP::$translation->for_translation( $set->project, $set, 0, array( 'priority' => array( '1' ) ) ) ) );
 336          $this->assertEquals( 0, count( GP::$translation->for_translation( $set->project, $set, 0, array( 'priority' => array( 1 ) ) ) ) );
 337  
 338          // Now let's modify the priority.
 339          $translation->original->priority = '1';
 340          $translation->original->status = '+active';
 341          $translation->original->save();
 342  
 343          // The modified original should now be found.
 344          $this->assertEquals( 1, count( GP::$translation->for_translation( $set->project, $set, 0 ) ) );
 345          $this->assertEquals( 1, count( GP::$translation->for_translation( $set->project, $set, 0, array( 'priority' => array( '1' ) ) ) ) );
 346          $this->assertEquals( 1, count( GP::$translation->for_translation( $set->project, $set, 0, array( 'priority' => array( '10' ) ) ) ) );
 347          $this->assertEquals( 0, count( GP::$translation->for_translation( $set->project, $set, 0, array( 'priority' => array( '0' ) ) ) ) );
 348  
 349          // Should also work with the hidden priority.
 350          $translation->original->priority = '-1';
 351          $translation->original->save();
 352  
 353          $this->assertEquals( 0, count( GP::$translation->for_translation( $set->project, $set, 0, array( 'priority' => array( '1' ) ) ) ) );
 354          $this->assertEquals( 1, count( GP::$translation->for_translation( $set->project, $set, 0, array( 'priority' => array( '-1' ) ) ) ) );
 355      }
 356  }


Generated: Thu Nov 21 01:01:07 2024 Cross-referenced by PHPXref 0.7.1