[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  
   3  class GP_Test_Project extends GP_UnitTestCase {
   4  
   5  	function test_empty_name() {
   6          $project = GP::$project->create( array( 'name' => '' ) );
   7          $verdict = $project->validate();
   8  
   9          $this->assertFalse( $verdict );
  10      }
  11  
  12  	function test_empty_slug() {
  13          $project = GP::$project->create( array( 'name' => 'Name', 'slug' => false ) );
  14          $verdict = $project->validate();
  15  
  16          $this->assertTrue( $verdict );
  17          $this->assertEquals( 'name', $project->path );
  18      }
  19  
  20  	function test_update_path() {
  21          $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root', 'path' => 'root' ) );
  22          // the slug is changed
  23          $p1 = GP::$project->create( array( 'name' => 'P1', 'slug' => 'cool', 'path' => 'root/p1', 'parent_project_id' => $root->id ) );
  24          $p2 = GP::$project->create( array( 'name' => 'P2', 'slug' => 'p2', 'path' => 'root/p1/p2', 'parent_project_id' => $p1->id ) );
  25          $p3 = GP::$project->create( array( 'name' => 'P3', 'slug' => 'p3', 'path' => 'root/p1/p2/p3', 'parent_project_id' => $p2->id ) );
  26          $p4 = GP::$project->create( array( 'name' => 'P4', 'slug' => 'p4', 'path' => 'root/p4/', 'parent_project_id' => $root->id ) );
  27          $p5 = GP::$project->create( array( 'name' => 'P5', 'slug' => 'p5', 'path' => 'root/p4/p5/', 'parent_project_id' => $p4->id ) );
  28          $p1->update_path();
  29          $p1->reload();
  30          $p2->reload();
  31          $p3->reload();
  32          $p4->reload();
  33          $p5->reload();
  34          $this->assertEquals( 'root/cool', $p1->path);
  35          $this->assertEquals( 'root/cool/p2', $p2->path);
  36          $this->assertEquals( 'root/cool/p2/p3', $p3->path);
  37          $this->assertEquals( 'root/p4', $p4->path);
  38          $this->assertEquals( 'root/p4/p5', $p5->path);
  39      }
  40  
  41  	function test_valid_path_on_create() {
  42          $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root', 'path' => 'root' ) );
  43          $p1 = GP::$project->create( array( 'name' => 'P1', 'slug' => 'p1', 'parent_project_id' => $root->id ) );
  44          $q = GP::$project->create( array( 'name' => 'Invader', 'slug' => 'invader', 'path' => '' ) );
  45          $p4 = GP::$project->create( array( 'name' => 'P4', 'slug' => 'p4', 'path' => 'root/p4/', 'parent_project_id' => $root->id ) );
  46          $p5 = GP::$project->create( array( 'name' => 'P5', 'slug' => 'p5', 'path' => 'root/p4/p5/', 'parent_project_id' => $p4->id ) );
  47          $root->reload();
  48          $p1->reload();
  49          $q->reload();
  50          $p4->reload();
  51          $p5->reload();
  52          $this->assertEquals( 'root', $root->path );
  53          $this->assertEquals( 'root/p1', $p1->path );
  54          $this->assertEquals( 'invader', $q->path );
  55          $this->assertEquals( 'root/p4', $p4->path);
  56          $this->assertEquals( 'root/p4/p5', $p5->path);
  57      }
  58  
  59  	function test_create_and_select() {
  60          $project = new GP_Project( array( 'name' => '@@@@', 'slug' => '' ) );
  61          $verdict = $project->validate();
  62  
  63          $this->assertFalse( $verdict );
  64      }
  65  
  66  	function test_save_no_args() {
  67          $p1 = GP::$project->create( array( 'name' => 'P1', 'slug' => 'p1', 'path' => 'p1' ) );
  68          $id = $p1->id;
  69          $p1->name = 'P2';
  70          $p1->save();
  71          $this->assertEquals( 'P2', $p1->name );
  72          $p1->reload();
  73          $this->assertEquals( 'P2', $p1->name );
  74          $this->assertEquals( 'P2', GP::$project->get( $id )->name );
  75      }
  76  
  77  	function test_reload() {
  78          global $wpdb;
  79          $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root' ) );
  80          $wpdb->update( $wpdb->gp_projects, array( 'name' => 'Buuu' ), array( 'id' => $root->id ) );
  81          $root->reload();
  82          $this->assertEquals( 'Buuu', $root->name );
  83      }
  84  
  85  	function test_path_to_root() {
  86          $root = $this->factory->project->create( array( 'name' => 'Root' ) );
  87          $sub = $this->factory->project->create( array( 'name' => 'Sub', 'parent_project_id' => $root->id ) );
  88          $subsub = $this->factory->project->create( array( 'name' => 'SubSub', 'parent_project_id' => $sub->id ) );
  89          $this->assertEquals( array( $subsub, $sub, $root ), $subsub->path_to_root() );
  90          $this->assertEquals( array( $sub, $root ), $sub->path_to_root() );
  91          $this->assertEquals( array( $root ), $root->path_to_root() );
  92      }
  93  
  94  	function test_by_path() {
  95          $root = $this->factory->project->create( array( 'name' => 'root' ) );
  96          $sub = $this->factory->project->create( array( 'name' => 'sub', 'parent_project_id' => $root->id ) );
  97          $this->assertEquals( $root->id, GP::$project->by_path( '/root' )->id );
  98          $this->assertEquals( $sub->id, GP::$project->by_path( '/root/sub/' )->id );
  99      }
 100  
 101  	function test_by_path_with_dots() {
 102          $root = $this->factory->project->create( array( 'name' => 'Root 1.0' ) );
 103          $sub = $this->factory->project->create( array( 'name' => 'Sub 1.0', 'parent_project_id' => $root->id ) );
 104          $this->assertEquals( $root->id, GP::$project->by_path( '/root-1.0' )->id );
 105          $this->assertEquals( $sub->id, GP::$project->by_path( '/root-1.0/sub-1.0/' )->id );
 106      }
 107  
 108  	function test_by_path_with_utf8mb4() {
 109          global $wpdb;
 110  
 111          if ( ! $wpdb->has_cap( 'utf8mb4' ) ) {
 112              $this->markTestSkipped( 'This test requires utf8mb4 support.' );
 113          }
 114  
 115          $root_slug  = gp_sanitize_slug( '😀' );
 116          $sub_1_slug = gp_sanitize_slug( "H€llo\xf0\x9f\x98\x88World¢" );
 117          $sub_2_slug = gp_sanitize_slug( '𝟜' );
 118  
 119          $root_slug_urldecoded       = urldecode( $root_slug );
 120          $sub_1_slug_urldecoded      = urldecode( $sub_1_slug );
 121          $sub_2_slug_slug_urldecoded = urldecode( $sub_2_slug );
 122  
 123          $root  = $this->factory->project->create( array( 'name' => '😀' ) );
 124          $sub_1 = $this->factory->project->create( array( 'name' => "H€llo\xf0\x9f\x98\x88World¢", 'parent_project_id' => $root->id ) );
 125          $sub_2 = $this->factory->project->create( array( 'name' => '𝟜', 'parent_project_id' => $root->id ) );
 126  
 127          $this->assertEquals( $root->id, GP::$project->by_path( '/' . $root_slug )->id );
 128          $this->assertEquals( $root->id, GP::$project->by_path( '/' . $root_slug_urldecoded . '/' )->id );
 129  
 130          $this->assertEquals( $sub_1->id, GP::$project->by_path( '/' . $root_slug . '/' . $sub_1_slug )->id );
 131          $this->assertEquals( $sub_1->id, GP::$project->by_path( '/' . $root_slug_urldecoded . '/' . $sub_1_slug_urldecoded )->id );
 132  
 133          $this->assertEquals( $sub_2->id, GP::$project->by_path( '/' . $root_slug . '/' . $sub_2_slug )->id );
 134          $this->assertEquals( $sub_2->id, GP::$project->by_path( '/' . $root_slug_urldecoded . '/' . $sub_2_slug_slug_urldecoded )->id );
 135      }
 136  
 137  	function test_regenerate_paths() {
 138          global $wpdb;
 139          $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root' ) );
 140          $sub  = $this->factory->project->create( array( 'name' => 'Sub', 'parent_project_id' => $root->id ) );
 141          $wpdb->update( $wpdb->gp_projects, array( 'path' => 'wrong-path' ), array( 'id' => $sub->id ) );
 142          $sub->reload();
 143          $sub->regenerate_paths();
 144          $sub->reload();
 145          $this->assertEquals( 'root/sub', $sub->path );
 146  
 147          // Run the same test a second time with a permalink structure that includes a trailing slash.
 148          $this->set_permalink_structure( GP_TESTS_PERMALINK_STRUCTURE_WITH_TRAILING_SLASH );
 149          $wpdb->update( $wpdb->gp_projects, array( 'path' => 'wrong-path' ), array( 'id' => $sub->id ) );
 150          $sub->reload();
 151          $sub->regenerate_paths();
 152          $sub->reload();
 153          $this->assertEquals( 'root/sub', $sub->path );
 154  
 155      }
 156  
 157  	function test_set_difference_from_same() {
 158          $p1 = $this->factory->project->create( array( 'name' => 'P1' ) );
 159          $p2 = $this->factory->project->create( array( 'name' => 'P2' ) );
 160  
 161          $difference = $p1->set_difference_from( $p2 );
 162  
 163          $this->assertEmpty( $difference['added'] );
 164          $this->assertEmpty( $difference['removed'] );
 165      }
 166  
 167  	function test_set_difference_from_difference() {
 168          $s1 = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'P1' ) );
 169          $s2 = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'nl' ), array( 'name' => 'P2' ) );
 170  
 171          $difference = $s1->project->set_difference_from( $s2->project );
 172  
 173          $this->assertEquals( $s2->id, $difference['added'][0]->id );
 174          $this->assertEquals( $s1->id, $difference['removed'][0]->id );
 175      }
 176  
 177  	function test_copy_originals_from() {
 178          $s1 = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'P1' ) );
 179          $s2 = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'nl' ), array( 'name' => 'P2' ) );
 180  
 181          $this->factory->translation->create_with_original_for_translation_set( $s1 );
 182  
 183          $s2->project->copy_originals_from( $s1->project->id );
 184  
 185          $s1_original = GP::$original->by_project_id( $s1->project->id );
 186          $s2_original = GP::$original->by_project_id( $s2->project->id );
 187          $s1_original = array_shift( $s1_original );
 188          $s2_original = array_shift( $s2_original );
 189  
 190          $this->assertNotEquals( $s1_original->id, $s2_original->id );
 191          $this->assertNotEquals( $s1_original->project_id, $s2_original->project_id );
 192          $this->assertEqualFields( $s2_original,
 193              array( 'singular' => $s1_original->singular, 'plural' => $s1_original->plural, 'references' => $s1_original->references, 'comment' =>$s1_original->comment, 'status' =>$s1_original->status, 'date_added' => $s1_original->date_added )
 194          );
 195      }
 196  
 197  	function test_sets_in_copy_sets_and_translations_from() {
 198          $s1 = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'P1' ) );
 199          $this->factory->translation->create_with_original_for_translation_set( $s1 );
 200  
 201          $branch = $this->factory->project->create( array( 'name' => 'branch' ) );
 202          $branch->copy_sets_and_translations_from( $s1->project->id );
 203  
 204          $difference = $branch->set_difference_from( $s1->project );
 205  
 206          $this->assertEmpty( $difference['added'] );
 207          $this->assertEmpty( $difference['removed'] );
 208  
 209      }
 210  
 211      function test_translations_in_copy_sets_and_translations_from() {
 212          $original = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'P1' ) );
 213          $this->factory->translation->create_with_original_for_translation_set( $original );
 214  
 215          $copy = $this->factory->project->create( array( 'name' => 'branch' ) );
 216          $copy->copy_originals_from( $original->project->id );
 217          $copy->copy_sets_and_translations_from( $original->project->id );
 218  
 219          $copy_set = GP::$translation_set->by_project_id( $copy->id );
 220          $copy_set = array_shift( $copy_set );
 221  
 222          $original_translation = GP::$translation->find( array( 'translation_set_id' => $original->id ) );
 223          $original_translation = array_shift( $original_translation );
 224          $copy_translation = GP::$translation->find( array( 'translation_set_id' => $copy_set->id ) );
 225          $copy_translation = array_shift( $copy_translation );
 226  
 227          $this->assertNotEquals( $original_translation->original_id, $copy_translation->original_id );
 228  
 229          $this->assertEqualFields( $copy_translation,
 230              array( 'translation_0' => $original_translation->translation_0 )
 231          );
 232  
 233      }
 234  
 235  	function test_branching_translation_sets(){
 236          $root_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'root' ) );
 237          $root = $root_set->project;
 238  
 239          $sub_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'Sub', 'parent_project_id' => $root->id ) );
 240          $sub = $sub_set->project;
 241  
 242          $subsub_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'SubSub', 'parent_project_id' => $sub->id ) );
 243          $subsub = $subsub_set->project;
 244  
 245          $this->factory->translation->create_with_original_for_translation_set( $root_set );
 246          $this->factory->translation->create_with_original_for_translation_set( $sub_set );
 247          $this->factory->translation->create_with_original_for_translation_set( $subsub_set );
 248  
 249          $branch = $this->factory->project->create( array( 'name' => 'branch' ) );
 250          $branch->duplicate_project_contents_from( $root );
 251  
 252          $branch_sub = $branch->sub_projects();
 253          $branch_sub = array_shift( $branch_sub );
 254          $branch_subsub = $branch_sub->sub_projects();
 255          $branch_subsub = array_shift( $branch_subsub );
 256  
 257          $difference_root = $root->set_difference_from( $branch );
 258          $difference_sub = $sub->set_difference_from( $branch_sub );
 259          $difference_subsub = $subsub->set_difference_from( $branch_subsub );
 260  
 261          $this->assertEmpty( $difference_root['added'] );
 262          $this->assertEmpty( $difference_root['removed'] );
 263  
 264          $this->assertEmpty( $difference_sub['added'] );
 265          $this->assertEmpty( $difference_sub['removed'] );
 266  
 267          $this->assertEmpty( $difference_subsub['added'] );
 268          $this->assertEmpty( $difference_subsub['removed'] );
 269      }
 270  
 271  	function test_branching_originals(){
 272          $root_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'root' ) );
 273          $root = $root_set->project;
 274  
 275          $sub_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'SubSub', 'parent_project_id' => $root->id ) );
 276          $sub = $sub_set->project;
 277  
 278          $this->factory->translation->create_with_original_for_translation_set( $root_set );
 279          $this->factory->translation->create_with_original_for_translation_set( $sub_set );
 280  
 281          $branch = $this->factory->project->create( array( 'name' => 'branch' ) );
 282          $branch->duplicate_project_contents_from( $root );
 283  
 284          $branch_sub = $branch->sub_projects();
 285          $branch_sub = array_shift( $branch_sub );
 286  
 287          $originals_root = GP::$original->by_project_id( $root->id );
 288          $originals_sub = GP::$original->by_project_id( $sub->id );
 289  
 290          $originals_branch = GP::$original->by_project_id( $branch->id );
 291          $originals_branch_sub = GP::$original->by_project_id( $branch_sub->id );
 292  
 293          $this->assertEquals( count( $originals_root ), count( $originals_branch ) );
 294          $this->assertEquals( count( $originals_sub ), count( $originals_branch_sub ) );
 295      }
 296  
 297  	function test_branching_paths(){
 298          $root_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'root' ) );
 299          $root = $root_set->project;
 300  
 301          $sub_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'sub', 'parent_project_id' => $root->id ) );
 302          $sub = $sub_set->project;
 303  
 304          $other_sub_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'other_sub', 'parent_project_id' => $root->id ) );
 305  
 306          $branch = $this->factory->project->create( array( 'name' => 'branch' ) );
 307          $branch->duplicate_project_contents_from( $root );
 308  
 309          $branch_sub = $branch->sub_projects();
 310          $branch_sub = array_shift( $branch_sub );
 311  
 312          $this->assertEquals( $root->path, 'root' );
 313          $this->assertEquals( $branch->path, 'branch' );
 314          $this->assertEquals( $sub->path, 'root/sub' );
 315          $this->assertEquals( $branch_sub->path, 'branch/sub' );
 316  
 317          $branch_other_sub = GP::$project->by_path('branch/other_sub');
 318          $this->assertNotEquals( false, $branch_other_sub );
 319      }
 320  
 321  	function test_delete() {
 322          $project = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root' ) );
 323  
 324          $pre_delete = GP::$project->find_one( array( 'id' => $project->id ) );
 325  
 326          $project->delete();
 327  
 328          $post_delete = GP::$project->find_one( array( 'id' => $project->id ) );
 329  
 330          $this->assertFalse( empty( $pre_delete ) );
 331          $this->assertNotEquals( $pre_delete, $post_delete );
 332      }
 333  
 334  	public function test_previous_state_is_passed_to_saved_action() {
 335          $project = $this->factory->project->create( array( 'name' => 'Before' ) );
 336          $initial_project = clone $project;
 337  
 338          $previous_project = null;
 339          $closure = function( $project_after, $project_before ) use ( &$previous_project ) {
 340              $previous_project = $project_before;
 341          };
 342  
 343          add_action( 'gp_project_saved', $closure, 10, 2 );
 344  
 345          $project->save( array( 'name' => 'After' ) );
 346  
 347          remove_action( 'gp_project_saved', $closure );
 348  
 349          $this->assertEquals( $initial_project, $previous_project );
 350          $this->assertEquals( $previous_project->name, 'Before' );
 351          $this->assertEquals( $project->name, 'After' );
 352      }
 353  
 354  	public function test_gp_locale_glossary_path_prefix_filter() {
 355          $custom_prefix = '/locale';
 356  
 357          $closure = function() use ( $custom_prefix ) {
 358              return $custom_prefix;
 359          };
 360  
 361          add_filter( 'gp_locale_glossary_path_prefix', $closure );
 362  
 363          $glossary = GP::$project->by_path( $custom_prefix );
 364  
 365          remove_filter( 'gp_locale_glossary_path_prefix', $closure );
 366  
 367          $this->assertInstanceOf( 'GP_Project', $glossary );
 368          $this->assertSame( 'Locale Glossary', $glossary->name );
 369      }
 370  }


Generated: Fri Apr 26 01:01:24 2024 Cross-referenced by PHPXref 0.7.1