| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace TractorCow\Fluent\Tests\Extension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\CMS\Model\SiteTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use TractorCow\Fluent\Extension\FluentSiteTreeExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use TractorCow\Fluent\State\FluentState; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedAnother; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedChild; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedParent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\UnlocalisedChild; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use TractorCow\Fluent\Tests\Extension\Stub\FluentStubObject; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | class FluentExtensionTest extends SapphireTest | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     protected static $extra_dataobjects = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         LocalisedAnother::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         LocalisedChild::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         LocalisedParent::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         UnlocalisedChild::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     protected static $required_extensions = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         SiteTree::class => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             FluentSiteTreeExtension::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         ], | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function testFluentLocaleAndFrontendAreAddedToDataQuery() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         FluentState::singleton() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             ->setLocale('test') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ->setIsFrontend(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $query = SiteTree::get()->dataQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->assertSame('test', $query->getQueryParam('Fluent.Locale')); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |         $this->assertTrue($query->getQueryParam('Fluent.IsFrontend')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function testGetLocalisedTable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         /** @var SiteTree|FluentSiteTreeExtension $page */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $page = new SiteTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->assertSame('SiteTree_Localised', $page->getLocalisedTable('SiteTree')); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |         $this->assertSame('SiteTree_Localised_FR', $page->getLocalisedTable('SiteTree', 'FR')); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function testGetLinkingMode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         // Does not have a canViewInLocale method, locale is not current | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $stub = new FluentStubObject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->assertSame('link', $stub->getLinkingMode('foo')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         // Does not have a canViewInLocale method, locale is current | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         FluentState::singleton()->setLocale('foo'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |         $this->assertSame('current', $stub->getLinkingMode('foo')); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function testGetLocalisedFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         // test data_include / data_exclude | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         // note: These parent fields should be all accessible from the child records as well | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $parent = new LocalisedParent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $parentLocalised = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'Title' => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             'Details' => 'Varchar(200)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $parentLocalised, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $parent->getLocalisedFields() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // test field_include / field_exclude | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $another = new LocalisedAnother(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 'Bastion' => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 'Data' => 'Varchar(100)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $another->getLocalisedFields() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $parentLocalised, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $another->getLocalisedFields(LocalisedParent::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         // Test translate directly | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $child = new LocalisedChild(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             [ 'Record' => 'Text' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $child->getLocalisedFields() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $parentLocalised, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $child->getLocalisedFields(LocalisedParent::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         // Test 'none' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $unlocalised = new UnlocalisedChild(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->assertEmpty($unlocalised->getLocalisedFields()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $parentLocalised, | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |             $unlocalised->getLocalisedFields(LocalisedParent::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |