| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dynamic\Locator\Tests\Page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dynamic\Locator\Location; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Dynamic\Locator\Model\LocationCategory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Dynamic\Locator\Page\Locator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Dynamic\Locator\Page\LocatorController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Core\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Dev\FunctionalTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Forms\FieldList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\Forms\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\View\ViewableData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Class LocatorControllerTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package Dynamic\Locator\Tests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class LocatorControllerTest extends FunctionalTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected static $fixture_file = 'locatorcontrollerfixture.yml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected static $use_draft_site = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     protected function setUp(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         parent::setUp(); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         Config::modify()->set(Locator::class, 'location_class', Location::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function testIndex() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $locator = $this->objFromFixture(Locator::class, 'locator1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $controller = LocatorController::create($locator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->assertInstanceOf(ViewableData::class, $controller->index($controller->getRequest())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function testXml() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         /** @var Locator $locator */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $locator = $this->objFromFixture(Locator::class, 'locator1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $page = $this->get($locator->Link('xml')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->assertEquals(200, $page->getStatusCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->assertEquals('application/xml', $page->getHeader('content-type')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $dom = new \DOMDocument(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         // true if it loads, false if it doesn't | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $valid = $dom->loadXML($page->getBody()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->assertTrue($valid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function testJson() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         /** @var Locator $locator */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $locator = $this->objFromFixture(Locator::class, 'locator1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $page = $this->get($locator->Link('json')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $this->assertEquals(200, $page->getStatusCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->assertEquals('application/json', $page->getHeader('content-type')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $json = json_decode($page->getBody()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // if it is null its not valid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->assertNotNull($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function testLocationSearch() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $locator = $this->objFromFixture(Locator::class, 'locator1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $object = LocatorController::create($locator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $form = $object->LocationSearch(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $this->assertInstanceOf(Form::class, $form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $category = $this->objFromFixture(LocationCategory::class, 'service'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $category2 = $this->objFromFixture(LocationCategory::class, 'manufacturing'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $locator->Categories()->add($category); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $locator->Categories()->add($category2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $form = $object->LocationSearch(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $fields = $form->Fields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->assertInstanceOf(FieldList::class, $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  |