| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Firesphere\SolrSearch\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Firesphere\SolrSearch\Helpers\DataResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Control\HTTPRequestBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Dev\Debug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\ORM\ArrayList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\ORM\DatabaseAdmin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\ORM\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\View\ArrayData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class DataResolverTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected static $fixture_file = 'tests/fixtures/DataResolver.yml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function assertEqualsDump() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         Debug::dump(func_get_args()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public static function setUpBeforeClass() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         parent::setUpBeforeClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         // This is a hack on my local dev to make fixtures populate temporary database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $conn = DB::get_conn(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $dbName = self::tempDB()->build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $conn->selectDatabase($dbName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $dbAdmin = new DatabaseAdmin(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $dbAdmin->setRequest(HTTPRequestBuilder::createFromEnvironment()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $dbAdmin->doBuild(true, true, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @expectedException Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @expectedExceptionMessage Class: stdClass, is not supported. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function testUnsupportedObjectException() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $obj = new stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $obj->Created = '2019-07-04 22:01:00'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $obj->Title = 'Hello generic class'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->assertEqualsDump($obj->Title, DataResolver::identify($obj, 'Title')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @expectedException Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @expectedExceptionMessage Cannot identify, "UnknownColumn" from class "TestPage" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function testCannotIdentifyExceptionForDataObject() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         DataResolver::identify($pageOne, 'UnknownColumn'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @expectedException Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @expectedExceptionMessage Cannot identify, "UnknownColumn" from class "ArrayData" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function testCannotIdentifyExceptionForArrayData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $arrayOne = new ArrayData($pageOne->toMap()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         DataResolver::identify($arrayOne, 'UnknownColumn'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @expectedException Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @expectedExceptionMessage Method, "SuperNice" not found on "DBDatetime" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function testMethodNotFoundFromDBField() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         DataResolver::identify($pageOne, 'Created.SuperNice'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @expectedException Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @expectedExceptionMessage Cannot identify, "Timestamp" from class "DBDatetime" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function testCannotIdentifyExceptionForDBField() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         DataResolver::identify($pageOne, 'Created.Nice.Timestamp'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function testDataObjectEmptyColumnIsToMap() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $objectOne = $this->objFromFixture(TestObject::class, 'objectOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->assertEquals($objectOne->toMap(), DataResolver::identify($objectOne)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $arrayOne = new ArrayData($objectOne->toMap()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $this->assertEquals($objectOne->toMap(), $arrayOne->toMap()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->assertEquals($arrayOne->toMap(), DataResolver::identify($arrayOne)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     public function testDataListEmptyColumnIsToNestedArray() | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         $objectOne = $this->objFromFixture(TestObject::class, 'objectOne'); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         $dataList = $objectOne->TestPages(); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         $this->assertEquals($dataList->toNestedArray(), DataResolver::identify($dataList)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function testDataObjectGetMethod() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->assertEquals($pageOne->getSalutation(), DataResolver::identify($pageOne, 'Salutation')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     public function testDataObjectAttributes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $mockDate = '2019-07-04 22:01:00'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $pageOne->Created = $mockDate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $pageOne->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $this->assertEquals($pageOne->Title, DataResolver::identify($pageOne, 'Title')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->assertEquals($mockDate, $pageOne->Created); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $this->assertEquals($mockDate, DataResolver::identify($pageOne, 'Created')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->assertEquals('Jul 4, 2019, 10:01 PM', DataResolver::identify($pageOne, 'Created.Nice')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $this->assertEquals('2019-07-04T22:01:00+00:00', DataResolver::identify($pageOne, 'Created.Rfc3339')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $this->assertEquals('1562277660', DataResolver::identify($pageOne, 'Created.Timestamp')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $this->assertEquals('1562277660', DataResolver::identify($pageOne, 'Created.getTimestamp')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $this->assertEquals('y-MM-dd HH:mm:ss', DataResolver::identify($pageOne, 'Created.ISOFormat')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     public function testDataArrayAttributes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $arrayOne = new ArrayData($pageOne->toMap()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $this->assertEquals($arrayOne->Title, DataResolver::identify($arrayOne, 'Title')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function testDataTraversal() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $mockDate = '2019-07-04 22:01:00'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $objectOne = $this->objFromFixture(TestObject::class, 'objectOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $pageOne = $this->objFromFixture(TestPage::class, 'pageOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $relationOne->Created = $mockDate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $relationOne->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $this->assertEquals($objectOne->toMap(), DataResolver::identify($pageOne, 'TestObject')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $this->assertEquals($objectOne->Title, DataResolver::identify($pageOne, 'TestObject.Title')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $this->assertEquals(0, DataResolver::identify($pageOne, 'TestObject.ShowInSearch')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $this->assertEquals('No', DataResolver::identify($pageOne, 'TestObject.ShowInSearch.Nice')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $this->assertEquals($objectOne->TestRelation()->toNestedArray(), DataResolver::identify($pageOne, 'TestObject.TestRelation')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $this->assertEquals($objectOne->TestRelation()->First()->toMap(), DataResolver::identify($pageOne, 'TestObject.TestRelation.First')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $this->assertEquals($relationOne->Title, DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Title')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $this->assertEquals($mockDate, DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $this->assertEquals($mockDate, $relationOne->Created); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $this->assertEquals('Jul 4, 2019, 10:01 PM', DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Nice')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $this->assertEquals('2019-07-04T22:01:00+00:00', DataResolver::identify($pageOne, 'TestObject.TestRelation.First.Created.Rfc3339')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $relationTwo = $this->objFromFixture(TestRelationObject::class, 'relationTwo'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $relationTwo->Created = '2019-07-05 23:05:00'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $relationTwo->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             ['Jul 4, 2019, 10:01 PM', 'Jul 5, 2019, 11:05 PM'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             DataResolver::identify($pageOne, 'TestObject.TestRelation.Created.Nice') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     public function testGetMethodReturnsArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $relationOne = $this->objFromFixture(TestRelationObject::class, 'relationOne'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $this->assertEquals($relationOne->FarmAnimals, DataResolver::identify($relationOne, 'FarmAnimals')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     public function testArrayList() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         $list = new ArrayList( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                 new ArrayData(['Title' => 'one']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 new ArrayData(['Title' => 'two']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 new ArrayData(['Title' => 'three']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         $this->assertEquals($list->toNestedArray(), DataResolver::identify($list)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         $this->assertEquals($list->first()->Title, DataResolver::identify($list, 'First.Title')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         $this->assertEquals($list->last()->Title, DataResolver::identify($list, 'Last.Title')); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 182 |  |  | } |