| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | class MultiDomainDomainTest extends SapphireTest | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |      * Set up some test domain data for testing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     public function setUp() | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |         parent::setUp(); | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |         Config::nest(); | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         Config::inst()->remove('MultiDomain', 'domains'); | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         Config::inst()->update('MultiDomain', 'domains', array( | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |             'primary' => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |                 'hostname' => 'example.com' | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |             'store' => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |                 'hostname' => 'example-store.com', | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |                 'resolves_to' => 'shop/store', | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |                 'allow' => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |                     'admin/*', | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |                     'Security/*', | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                     'my-custom-webhook/' | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |                 ) | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             'configurable' => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |                 'hostname' => 'MY_CONSTANT_HOSTNAME' | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             'forceful' => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |                 'hostname' => 'forced.com', | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |                 'force' => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                     'buy-now/*' | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                 ) | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Test that a hostname defined in a constant will override the default configuration, otherwise the default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * configuration for the domain is returned | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function testGetHostname() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $configurableDomain = MultiDomain::get_domain('configurable'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         define('MY_CONSTANT_HOSTNAME', 'I am a constant'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->assertSame('I am a constant', $configurableDomain->getHostname()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $storeDomain = MultiDomain::get_domain('store'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->assertSame('example-store.com', $storeDomain->getHostname()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Test that the domain's "resolves to" property is returned for the URL if it is defined, otherwise null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function testGetUrl() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $primaryDomain = MultiDomain::get_domain('primary'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->assertNull($primaryDomain->getURL()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $storeDomain = MultiDomain::get_domain('store'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->assertSame('shop/store', $storeDomain->getURL()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Test that a domain can be identified as the primary domain or otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function testIsPrimary() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->assertTrue(MultiDomain::get_primary_domain()->isPrimary()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->assertFalse(MultiDomain::get_domain('store')->isPrimary()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * When the request URI matches one of the allowed rules for a domain, the isActive method should return false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function testIsActiveReturnsFalseWhenRequestUriIsAllowedPath() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $domain = MultiDomain::get_domain('store'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $domain->setRequestUri('/Security/login'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->assertFalse($domain->isActive()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * When a subdomain is "allowed" and is requested, subdomains should be allowed through "isActive" as well | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * as the primary domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function testSubdomainsAllowedInIsActiveWhenConfigured() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         Config::inst()->update('MultiDomain', 'allow_subdomains', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $domain = MultiDomain::get_domain('store') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ->setRequestUri('/some/page') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ->setHttpHost('api.example-store.com'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->assertTrue($domain->isActive()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * The default behaviour would be that if the current host from the request matchese that of the domain model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * then isActive should be true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function testReturnActiveIfCurrentHostMatchesDomainsHostname() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $domain = MultiDomain::get_domain('primary') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ->setRequestUri('/another/page') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             ->setHttpHost('example.com'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->assertTrue($domain->isActive()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * getNativeUrl should not be used on the primary domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @expectedException Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @expectedExceptionMessage Cannot convert a native URL on the primary domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function testGetNativeUrlThrowsExceptionOnPrimaryDomain() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         MultiDomain::get_primary_domain()->getNativeUrl('foo'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * Test that a URL segment can be added to the domain's URL and returned as a "native URL" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     public function testGetNativeUrl() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $domain = MultiDomain::get_domain('store'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $this->assertSame('shop/store/foo/bar', $domain->getNativeUrl('foo/bar')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * "Allowed" and "forced" URLs should just be returned from getNativeUrl as is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     public function testGetNativeUrlReturnsInputWhenUrlIsAllowedOrForced() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $domain = MultiDomain::get_domain('store'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $this->assertSame('my-custom-webhook/', $domain->getNativeUrl('my-custom-webhook/')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $domain = MultiDomain::get_domain('forceful'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $this->assertSame('buy-now/whatever', $domain->getNativeUrl('buy-now/whatever')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * The primary domain and "allowed" route matches should be returned as it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     public function testGetVanityUrlReturnsInputWhenUrlIsAllowedOrIsPrimaryDomain() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $this->assertSame('/pages/info', MultiDomain::get_primary_domain()->getVanityUrl('/pages/info')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $this->assertSame('/Security/login', MultiDomain::get_domain('store')->getVanityUrl('/Security/login')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * Non-primary domains and un-allowed route matches should be returned without their URL for vanity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     public function testGetVanityUrl() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $this->assertSame('partners/', MultiDomain::get_domain('store')->getVanityUrl('shop/store/partners/')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $this->assertSame('foo/bar', MultiDomain::get_domain('store')->getVanityUrl('shop/store/foo/bar')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     public function tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         Config::unnest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         parent::tearDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  | 
            
                        
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.