[ 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_regenerate_paths() {
  95          global $wpdb;
  96          $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root' ) );
  97          $sub  = $this->factory->project->create( array( 'name' => 'Sub', 'parent_project_id' => $root->id ) );
  98          $wpdb->update( $wpdb->gp_projects, array( 'path' => 'wrong-path' ), array( 'id' => $sub->id ) );
  99          $sub->reload();
 100          $sub->regenerate_paths();
 101          $sub->reload();
 102          $this->assertEquals( 'root/sub', $sub->path );
 103  
 104          // Run the same test a second time with a permalink structure that includes a trailing slash.
 105          $this->set_permalink_structure( GP_TESTS_PERMALINK_STRUCTURE_WITH_TRAILING_SLASH );
 106          $wpdb->update( $wpdb->gp_projects, array( 'path' => 'wrong-path' ), array( 'id' => $sub->id ) );
 107          $sub->reload();
 108          $sub->regenerate_paths();
 109          $sub->reload();
 110          $this->assertEquals( 'root/sub', $sub->path );
 111  
 112      }
 113  
 114  	function test_set_difference_from_same() {
 115          $p1 = $this->factory->project->create( array( 'name' => 'P1' ) );
 116          $p2 = $this->factory->project->create( array( 'name' => 'P2' ) );
 117  
 118          $difference = $p1->set_difference_from( $p2 );
 119  
 120          $this->assertEmpty( $difference['added'] );
 121          $this->assertEmpty( $difference['removed'] );
 122      }
 123  
 124  	function test_set_difference_from_difference() {
 125          $s1 = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'P1' ) );
 126          $s2 = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'nl' ), array( 'name' => 'P2' ) );
 127  
 128          $difference = $s1->project->set_difference_from( $s2->project );
 129  
 130          $this->assertEquals( $s2->id, $difference['added'][0]->id );
 131          $this->assertEquals( $s1->id, $difference['removed'][0]->id );
 132      }
 133  
 134  	function test_copy_originals_from() {
 135          $s1 = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'P1' ) );
 136          $s2 = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'nl' ), array( 'name' => 'P2' ) );
 137  
 138          $this->factory->translation->create_with_original_for_translation_set( $s1 );
 139  
 140          $s2->project->copy_originals_from( $s1->project->id );
 141  
 142          $s1_original = GP::$original->by_project_id( $s1->project->id );
 143          $s2_original = GP::$original->by_project_id( $s2->project->id );
 144          $s1_original = array_shift( $s1_original );
 145          $s2_original = array_shift( $s2_original );
 146  
 147          $this->assertNotEquals( $s1_original->id, $s2_original->id );
 148          $this->assertNotEquals( $s1_original->project_id, $s2_original->project_id );
 149          $this->assertEqualFields( $s2_original,
 150              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 )
 151          );
 152      }
 153  
 154  	function test_sets_in_copy_sets_and_translations_from() {
 155          $s1 = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'P1' ) );
 156          $this->factory->translation->create_with_original_for_translation_set( $s1 );
 157  
 158          $branch = $this->factory->project->create( array( 'name' => 'branch' ) );
 159          $branch->copy_sets_and_translations_from( $s1->project->id );
 160  
 161          $difference = $branch->set_difference_from( $s1->project );
 162  
 163          $this->assertEmpty( $difference['added'] );
 164          $this->assertEmpty( $difference['removed'] );
 165  
 166      }
 167  
 168      function test_translations_in_copy_sets_and_translations_from() {
 169          $original = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'P1' ) );
 170          $this->factory->translation->create_with_original_for_translation_set( $original );
 171  
 172          $copy = $this->factory->project->create( array( 'name' => 'branch' ) );
 173          $copy->copy_originals_from( $original->project->id );
 174          $copy->copy_sets_and_translations_from( $original->project->id );
 175  
 176          $copy_set = GP::$translation_set->by_project_id( $copy->id );
 177          $copy_set = array_shift( $copy_set );
 178  
 179          $original_translation = GP::$translation->find( array( 'translation_set_id' => $original->id ) );
 180          $original_translation = array_shift( $original_translation );
 181          $copy_translation = GP::$translation->find( array( 'translation_set_id' => $copy_set->id ) );
 182          $copy_translation = array_shift( $copy_translation );
 183  
 184          $this->assertNotEquals( $original_translation->original_id, $copy_translation->original_id );
 185  
 186          $this->assertEqualFields( $copy_translation,
 187              array( 'translation_0' => $original_translation->translation_0 )
 188          );
 189  
 190      }
 191  
 192  	function test_branching_translation_sets(){
 193          $root_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'root' ) );
 194          $root = $root_set->project;
 195  
 196          $sub_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'Sub', 'parent_project_id' => $root->id ) );
 197          $sub = $sub_set->project;
 198  
 199          $subsub_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'SubSub', 'parent_project_id' => $sub->id ) );
 200          $subsub = $subsub_set->project;
 201  
 202          $this->factory->translation->create_with_original_for_translation_set( $root_set );
 203          $this->factory->translation->create_with_original_for_translation_set( $sub_set );
 204          $this->factory->translation->create_with_original_for_translation_set( $subsub_set );
 205  
 206          $branch = $this->factory->project->create( array( 'name' => 'branch' ) );
 207          $branch->duplicate_project_contents_from( $root );
 208  
 209          $branch_sub = $branch->sub_projects();
 210          $branch_sub = array_shift( $branch_sub );
 211          $branch_subsub = $branch_sub->sub_projects();
 212          $branch_subsub = array_shift( $branch_subsub );
 213  
 214          $difference_root = $root->set_difference_from( $branch );
 215          $difference_sub = $sub->set_difference_from( $branch_sub );
 216          $difference_subsub = $subsub->set_difference_from( $branch_subsub );
 217  
 218          $this->assertEmpty( $difference_root['added'] );
 219          $this->assertEmpty( $difference_root['removed'] );
 220  
 221          $this->assertEmpty( $difference_sub['added'] );
 222          $this->assertEmpty( $difference_sub['removed'] );
 223  
 224          $this->assertEmpty( $difference_subsub['added'] );
 225          $this->assertEmpty( $difference_subsub['removed'] );
 226      }
 227  
 228  	function test_branching_originals(){
 229          $root_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'root' ) );
 230          $root = $root_set->project;
 231  
 232          $sub_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'SubSub', 'parent_project_id' => $root->id ) );
 233          $sub = $sub_set->project;
 234  
 235          $this->factory->translation->create_with_original_for_translation_set( $root_set );
 236          $this->factory->translation->create_with_original_for_translation_set( $sub_set );
 237  
 238          $branch = $this->factory->project->create( array( 'name' => 'branch' ) );
 239          $branch->duplicate_project_contents_from( $root );
 240  
 241          $branch_sub = $branch->sub_projects();
 242          $branch_sub = array_shift( $branch_sub );
 243  
 244          $originals_root = GP::$original->by_project_id( $root->id );
 245          $originals_sub = GP::$original->by_project_id( $sub->id );
 246  
 247          $originals_branch = GP::$original->by_project_id( $branch->id );
 248          $originals_branch_sub = GP::$original->by_project_id( $branch_sub->id );
 249  
 250          $this->assertEquals( count( $originals_root ), count( $originals_branch ) );
 251          $this->assertEquals( count( $originals_sub ), count( $originals_branch_sub ) );
 252      }
 253  
 254  	function test_branching_paths(){
 255          $root_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'root' ) );
 256          $root = $root_set->project;
 257  
 258          $sub_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'sub', 'parent_project_id' => $root->id ) );
 259          $sub = $sub_set->project;
 260  
 261          $other_sub_set = $this->factory->translation_set->create_with_project_and_locale( array( 'locale' => 'bg' ), array( 'name' => 'other_sub', 'parent_project_id' => $root->id ) );
 262  
 263          $branch = $this->factory->project->create( array( 'name' => 'branch' ) );
 264          $branch->duplicate_project_contents_from( $root );
 265  
 266          $branch_sub = $branch->sub_projects();
 267          $branch_sub = array_shift( $branch_sub );
 268  
 269          $this->assertEquals( $root->path, 'root' );
 270          $this->assertEquals( $branch->path, 'branch' );
 271          $this->assertEquals( $sub->path, 'root/sub' );
 272          $this->assertEquals( $branch_sub->path, 'branch/sub' );
 273  
 274          $branch_other_sub = GP::$project->by_path('branch/other_sub');
 275          $this->assertNotEquals( false, $branch_other_sub );
 276      }
 277  
 278  	function test_delete() {
 279          $project = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root' ) );
 280  
 281          $pre_delete = GP::$project->find_one( array( 'id' => $project->id ) );
 282  
 283          $project->delete();
 284  
 285          $post_delete = GP::$project->find_one( array( 'id' => $project->id ) );
 286  
 287          $this->assertFalse( empty( $pre_delete ) );
 288          $this->assertNotEquals( $pre_delete, $post_delete );
 289      }
 290  
 291  	public function test_previous_state_is_passed_to_saved_action() {
 292          $project = $this->factory->project->create( array( 'name' => 'Before' ) );
 293          $initial_project = clone $project;
 294  
 295          $previous_project = null;
 296          $closure = function( $project_after, $project_before ) use ( &$previous_project ) {
 297              $previous_project = $project_before;
 298          };
 299  
 300          add_action( 'gp_project_saved', $closure, 10, 2 );
 301  
 302          $project->save( array( 'name' => 'After' ) );
 303  
 304          remove_action( 'gp_project_saved', $closure );
 305  
 306          $this->assertEquals( $initial_project, $previous_project );
 307          $this->assertEquals( $previous_project->name, 'Before' );
 308          $this->assertEquals( $project->name, 'After' );
 309      }
 310  
 311  	public function test_gp_locale_glossary_path_prefix_filter() {
 312          $custom_prefix = '/locale';
 313  
 314          $closure = function() use ( $custom_prefix ) {
 315              return $custom_prefix;
 316          };
 317  
 318          add_filter( 'gp_locale_glossary_path_prefix', $closure );
 319  
 320          $glossary = GP::$project->by_path( $custom_prefix );
 321  
 322          remove_filter( 'gp_locale_glossary_path_prefix', $closure );
 323  
 324          $this->assertInstanceOf( 'GP_Project', $glossary );
 325          $this->assertSame( 'Locale Glossary', $glossary->name );
 326      }
 327  }


Generated: Wed Nov 20 01:01:59 2019 Cross-referenced by PHPXref 0.7.1