| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\CKANRegistry\Tests\Forms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\CKANRegistry\Forms\ResourceLocatorField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\CKANRegistry\Model\Resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\CKANRegistry\Page\CKANRegistryPage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\CKANRegistry\Service\ResourcePopulatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class ResourceLocatorFieldTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $usesDatabase = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var ResourceLocatorField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->field = new ResourceLocatorField('my-field'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ->setEndpointFieldName('Endpoint') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ->setDatasetFieldName('DataSet') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             ->setResourceFieldName('Identifier'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         // Mock the field populator, in case an action we perform in a unit test tries to contact the mock API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $populator = $this->createMock(ResourcePopulatorInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         Injector::inst()->registerService($populator, ResourcePopulatorInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function testConstruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $field = new ResourceLocatorField('my-field', 'My field', null, 'http://example.com'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->assertSame('http://example.com', $field->getDefaultEndpoint()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->assertContains('Connect to a data source from', $field->getDescription()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function testSetValueWithArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->field->setValue(['foo' => 'bar']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->assertSame(['foo' => 'bar'], $this->field->Value(), 'Array values should pass through'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function testSetValueWithNonDataObject() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $value = new stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $value->foo = 'bar'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->field->setValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->assertNull($this->field->Value(), 'Non DataObject instances should set a null value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function testSetValueWithNoDataSet() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ->setDatasetFieldName('NonExistentField') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ->setValue($this->getResource()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->assertNull($this->field->Value(), 'Missing data sets should set a null value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function testSetValueWithNoEndpointFallsBackToDefaultEndpoint() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ->setEndpointFieldName('NonExistentField') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ->setDefaultEndpoint('http://example.com') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ->setValue($this->getResource()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->assertContains( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             'http://example.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $this->field->Value(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'Missing endpoint data on the model should fall back to the configured default endpoint' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function testSetValueWithNoEndpointAndNoDefaultReturnsNull() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         ResourceLocatorField::config()->set('default_endpoint', ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             ->setEndpointFieldName('NonExistentField') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             ->setDefaultEndpoint(null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ->setValue($this->getResource()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $this->assertNull( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $this->field->Value(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             'Missing endpoint data on the model, and missing default endpoint should return null' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function testSetValueWithExpectedDataStructure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $this->field->setValue($this->getResource()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $result = $this->field->Value(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->assertContains('http://example.com', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $this->assertContains('foo-bar', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->assertContains('123-456', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function testSetSubmittedValue() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $value = json_encode([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             'endpoint' => 'https://www.silverstripe.org', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             'dataset' => 'monkeys', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             'resource' => 'feeding', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $this->field->setSubmittedValue($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->assertContains('https://www.silverstripe.org', $this->field->Value()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function testDataValue() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $this->field->setValue($this->getResource()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $result = $this->field->dataValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $this->assertJson($result, 'dataValue() should return JSON'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->assertContains('foo-bar', $result, 'Serialised result should contain value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |     public function testSaveIntoWithNoNameReturnsEarly() | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |         $this->field->setName(null); | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         $this->assertNull($this->field->saveInto($this->getPage())); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @expectedException InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @expectedExceptionMessageRegExp /Could not determine where to save the value of/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     public function testSaveIntoWithInvalidRelationshipNameThrowsException() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $this->field->setName('UnicornRelationship'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $this->field->saveInto($this->getPage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     public function testSaveIntoSetsRelationshipFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $page = $this->getPage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $this->field->setName('DataResource'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $this->field->setValue($this->getResource()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $this->field->saveInto($page); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $this->assertNotEmpty($page->DataResourceID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $this->assertSame('http://example.com', $page->DataResource()->Endpoint); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->assertSame('foo-bar', $page->DataResource()->DataSet); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $this->assertSame('123-456', $page->DataResource()->Identifier); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     public function testDefaultEndpointUsesConfigIfNotProvided() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $this->field->setDefaultEndpoint('foo'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $this->assertSame('foo', $this->field->getDefaultEndpoint()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $this->field->setDefaultEndpoint(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         ResourceLocatorField::config()->set('default_endpoint', 'bar'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $this->assertSame('bar', $this->field->getDefaultEndpoint()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     public function testSiteNameUsesTranslatedDefaultIfNotProvided() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         $this->field->setSiteName('My cool website'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         $this->assertSame('My cool website', $this->field->getSiteName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         $this->field->setSiteName(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         $this->assertSame('a CKAN website', $this->field->getSiteName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * Returns a Resource object for testing values in the field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      * @return Resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     protected function getResource() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         $resource = new Resource(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         $resource->Endpoint = 'http://example.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         $resource->DataSet = 'foo-bar'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         $resource->Identifier = '123-456'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         return $resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * Returns a CKAN Page object for testing values in the field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * @return CKANRegistryPage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     protected function getPage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         $page = new CKANRegistryPage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         $page->Title = 'My CKAN Page'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         return $page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 204 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 205 |  |  |  | 
            
                        
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.