diff --git a/code/MobileBrowserDetector.php b/code/MobileBrowserDetector.php index 6dc1501..2977c45 100644 --- a/code/MobileBrowserDetector.php +++ b/code/MobileBrowserDetector.php @@ -9,159 +9,174 @@ * * @package mobile */ -class MobileBrowserDetector { +class MobileBrowserDetector +{ - /** - * List of known mobiles, found in the HTTP_USER_AGENT variable - * @see MobileBrowserDetector::is_mobile() for how they're used. - * - * @return array - */ - private static function mobile_index_list() { - return explode(',', '1207,3gso,4thp,501i,502i,503i,504i,505i,506i,6310,6590,770s,802s,a wa,acer,acs-,airn,alav,asus,attw,au-m,aur ,aus ,abac,acoo,aiko,alco,alca,amoi,anex,anny,anyw,aptu,arch,argo,bell,bird,bw-n,bw-u,beck,benq,bilb,blac,c55/,cdm-,chtm,capi,comp,cond,craw,dall,dbte,dc-s,dica,ds-d,ds12,dait,devi,dmob,doco,dopo,el49,erk0,esl8,ez40,ez60,ez70,ezos,ezze,elai,emul,eric,ezwa,fake,fly-,fly_,g-mo,g1 u,g560,gf-5,grun,gene,go.w,good,grad,hcit,hd-m,hd-p,hd-t,hei-,hp i,hpip,hs-c,htc ,htc-,htca,htcg,htcp,htcs,htct,htc_,haie,hita,huaw,hutc,i-20,i-go,i-ma,i230,iac,iac-,iac/,ig01,im1k,inno,iris,jata,java,kddi,kgt,kgt/,kpt ,kwc-,klon,lexi,lg g,lg-a,lg-b,lg-c,lg-d,lg-f,lg-g,lg-k,lg-l,lg-m,lg-o,lg-p,lg-s,lg-t,lg-u,lg-w,lg/k,lg/l,lg/u,lg50,lg54,lge-,lge/,lynx,leno,m1-w,m3ga,m50/,maui,mc01,mc21,mcca,medi,meri,mio8,mioa,mo01,mo02,mode,modo,mot ,mot-,mt50,mtp1,mtv ,mate,maxo,merc,mits,mobi,motv,mozz,n100,n101,n102,n202,n203,n300,n302,n500,n502,n505,n700,n701,n710,nec-,nem-,newg,neon,netf,noki,nzph,o2 x,o2-x,opwv,owg1,opti,oran,p800,pand,pg-1,pg-2,pg-3,pg-6,pg-8,pg-c,pg13,phil,pn-2,pt-g,palm,pana,pire,pock,pose,psio,qa-a,qc-2,qc-3,qc-5,qc-7,qc07,qc12,qc21,qc32,qc60,qci-,qwap,qtek,r380,r600,raks,rim9,rove,s55/,sage,sams,sc01,sch-,scp-,sdk/,se47,sec-,sec0,sec1,semc,sgh-,shar,sie-,sk-0,sl45,slid,smb3,smt5,sp01,sph-,spv ,spv-,sy01,samm,sany,sava,scoo,send,siem,smar,smit,soft,sony,t-mo,t218,t250,t600,t610,t618,tcl-,tdg-,telm,tim-,ts70,tsm-,tsm3,tsm5,tx-9,tagt,talk,teli,topl,tosh,up.b,upg1,utst,v400,v750,veri,vk-v,vk40,vk50,vk52,vk53,vm40,vx98,virg,vite,voda,vulc,w3c ,w3c-,wapj,wapp,wapu,wapm,wig ,wapi,wapr,wapv,wapy,wapa,waps,wapt,winc,winw,wonu,x700,xda2,xdag,yas-,your,zte-,zeto,aste,audi,avan,blaz,brew,brvw,bumb,ccwa,cell,cldc,cmd-,dang,eml2,fetc,hipt,http,ibro,idea,ikom,ipaq,jbro,jemu,jigs,keji,kyoc,kyok,libw,m-cr,midp,mmef,moto,mwbp,mywa,newt,nok6,o2im,pant,pdxg,play,pluc,port,prox,rozo,sama,seri,smal,symb,treo,upsi,vx52,vx53,vx60,vx61,vx70,vx80,vx81,vx83,vx85,wap-,webc,whit,wmlb,xda-'); - } + /** + * List of known mobiles, found in the HTTP_USER_AGENT variable + * @see MobileBrowserDetector::is_mobile() for how they're used. + * + * @return array + */ + private static function mobile_index_list() + { + return explode(',', '1207,3gso,4thp,501i,502i,503i,504i,505i,506i,6310,6590,770s,802s,a wa,acer,acs-,airn,alav,asus,attw,au-m,aur ,aus ,abac,acoo,aiko,alco,alca,amoi,anex,anny,anyw,aptu,arch,argo,bell,bird,bw-n,bw-u,beck,benq,bilb,blac,c55/,cdm-,chtm,capi,comp,cond,craw,dall,dbte,dc-s,dica,ds-d,ds12,dait,devi,dmob,doco,dopo,el49,erk0,esl8,ez40,ez60,ez70,ezos,ezze,elai,emul,eric,ezwa,fake,fly-,fly_,g-mo,g1 u,g560,gf-5,grun,gene,go.w,good,grad,hcit,hd-m,hd-p,hd-t,hei-,hp i,hpip,hs-c,htc ,htc-,htca,htcg,htcp,htcs,htct,htc_,haie,hita,huaw,hutc,i-20,i-go,i-ma,i230,iac,iac-,iac/,ig01,im1k,inno,iris,jata,java,kddi,kgt,kgt/,kpt ,kwc-,klon,lexi,lg g,lg-a,lg-b,lg-c,lg-d,lg-f,lg-g,lg-k,lg-l,lg-m,lg-o,lg-p,lg-s,lg-t,lg-u,lg-w,lg/k,lg/l,lg/u,lg50,lg54,lge-,lge/,lynx,leno,m1-w,m3ga,m50/,maui,mc01,mc21,mcca,medi,meri,mio8,mioa,mo01,mo02,mode,modo,mot ,mot-,mt50,mtp1,mtv ,mate,maxo,merc,mits,mobi,motv,mozz,n100,n101,n102,n202,n203,n300,n302,n500,n502,n505,n700,n701,n710,nec-,nem-,newg,neon,netf,noki,nzph,o2 x,o2-x,opwv,owg1,opti,oran,p800,pand,pg-1,pg-2,pg-3,pg-6,pg-8,pg-c,pg13,phil,pn-2,pt-g,palm,pana,pire,pock,pose,psio,qa-a,qc-2,qc-3,qc-5,qc-7,qc07,qc12,qc21,qc32,qc60,qci-,qwap,qtek,r380,r600,raks,rim9,rove,s55/,sage,sams,sc01,sch-,scp-,sdk/,se47,sec-,sec0,sec1,semc,sgh-,shar,sie-,sk-0,sl45,slid,smb3,smt5,sp01,sph-,spv ,spv-,sy01,samm,sany,sava,scoo,send,siem,smar,smit,soft,sony,t-mo,t218,t250,t600,t610,t618,tcl-,tdg-,telm,tim-,ts70,tsm-,tsm3,tsm5,tx-9,tagt,talk,teli,topl,tosh,up.b,upg1,utst,v400,v750,veri,vk-v,vk40,vk50,vk52,vk53,vm40,vx98,virg,vite,voda,vulc,w3c ,w3c-,wapj,wapp,wapu,wapm,wig ,wapi,wapr,wapv,wapy,wapa,waps,wapt,winc,winw,wonu,x700,xda2,xdag,yas-,your,zte-,zeto,aste,audi,avan,blaz,brew,brvw,bumb,ccwa,cell,cldc,cmd-,dang,eml2,fetc,hipt,http,ibro,idea,ikom,ipaq,jbro,jemu,jigs,keji,kyoc,kyok,libw,m-cr,midp,mmef,moto,mwbp,mywa,newt,nok6,o2im,pant,pdxg,play,pluc,port,prox,rozo,sama,seri,smal,symb,treo,upsi,vx52,vx53,vx60,vx61,vx70,vx80,vx81,vx83,vx85,wap-,webc,whit,wmlb,xda-'); + } - public static function is_android() { - return (stripos($_SERVER['HTTP_USER_AGENT'], 'android') !== false) ? true : false; - } + public static function is_android() + { + return (stripos($_SERVER['HTTP_USER_AGENT'], 'android') !== false) ? true : false; + } - public static function is_iphone() { - return (preg_match('/(ipod|iphone)/i', $_SERVER['HTTP_USER_AGENT'])) ? true : false; - } + public static function is_iphone() + { + return (preg_match('/(ipod|iphone)/i', $_SERVER['HTTP_USER_AGENT'])) ? true : false; + } - public static function is_opera_mini() { - return (stripos($_SERVER['HTTP_USER_AGENT'], 'opera mini') !== false) ? true : false; - } + public static function is_opera_mini() + { + return (stripos($_SERVER['HTTP_USER_AGENT'], 'opera mini') !== false) ? true : false; + } - public static function is_blackberry() { - return (stripos($_SERVER['HTTP_USER_AGENT'], 'blackberry') !== false) ? true : false; - } + public static function is_blackberry() + { + return (stripos($_SERVER['HTTP_USER_AGENT'], 'blackberry') !== false) ? true : false; + } - public static function is_palm() { - return (preg_match('/(palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)/i', $_SERVER['HTTP_USER_AGENT'])) ? true : false; - } + public static function is_palm() + { + return (preg_match('/(palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)/i', $_SERVER['HTTP_USER_AGENT'])) ? true : false; + } - public static function is_windows() { - return (preg_match('/(windows ce; ppc;|windows ce; smartphone;|windows ce; iemobile)/i', $_SERVER['HTTP_USER_AGENT'])) ? true : false; - } + public static function is_windows() + { + return (preg_match('/(windows ce; ppc;|windows ce; smartphone;|windows ce; iemobile)/i', $_SERVER['HTTP_USER_AGENT'])) ? true : false; + } - public static function is_win_phone() { - return (stripos($_SERVER['HTTP_USER_AGENT'], 'Windows Phone OS') !== false) ? true : false; - } + public static function is_win_phone() + { + return (stripos($_SERVER['HTTP_USER_AGENT'], 'Windows Phone OS') !== false) ? true : false; + } - /** - * Rough detection of "mobile" browsers based on their user agent strings. - * Includes tablets (see {@link is_tablet()}). Sets HTTP cache headers. - * - * CAUTION: Does NOT detect Windows 8 tablets, since there's no user-agent distinction between - * tablets and desktops in Windows 8. - * - * @see http://mobiforge.com/developing/story/setting-http-headers-advise-transcoding-proxies - * - * @param String User agent (defaults to $_SERVER) - * @return bool - */ - public static function is_mobile($agent = null) { - $isMobile = false; - if(!$agent) $agent = $_SERVER['HTTP_USER_AGENT']; - $accept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : ''; + /** + * Rough detection of "mobile" browsers based on their user agent strings. + * Includes tablets (see {@link is_tablet()}). Sets HTTP cache headers. + * + * CAUTION: Does NOT detect Windows 8 tablets, since there's no user-agent distinction between + * tablets and desktops in Windows 8. + * + * @see http://mobiforge.com/developing/story/setting-http-headers-advise-transcoding-proxies + * + * @param String User agent (defaults to $_SERVER) + * @return bool + */ + public static function is_mobile($agent = null) + { + $isMobile = false; + if (!$agent) { + $agent = $_SERVER['HTTP_USER_AGENT']; + } + $accept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : ''; - switch(true) { - case(self::is_iphone()): - $isMobile = true; - break; - case(self::is_android()): - $isMobile = true; - break; - case(self::is_opera_mini()): - $isMobile = true; - break; - case(self::is_blackberry()): - $isMobile = true; - break; - case(self::is_palm()): - $isMobile = true; - break; - case(self::is_win_phone()): - $isMobile = true; - break; - case(self::is_windows()): - $isMobile = true; - break; - case(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|pda|psp|treo)/i', $agent)): - $isMobile = true; - break; - case((strpos($accept, 'text/vnd.wap.wml') !== false) || (strpos($accept, 'application/vnd.wap.xhtml+xml') !== false)): - $isMobile = true; - break; - case(isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])): - $isMobile = true; - break; - case(in_array(strtolower(substr($agent, 0, 4)), self::mobile_index_list())): - $isMobile = true; - break; - } + switch (true) { + case(self::is_iphone()): + $isMobile = true; + break; + case(self::is_android()): + $isMobile = true; + break; + case(self::is_opera_mini()): + $isMobile = true; + break; + case(self::is_blackberry()): + $isMobile = true; + break; + case(self::is_palm()): + $isMobile = true; + break; + case(self::is_win_phone()): + $isMobile = true; + break; + case(self::is_windows()): + $isMobile = true; + break; + case(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|pda|psp|treo)/i', $agent)): + $isMobile = true; + break; + case((strpos($accept, 'text/vnd.wap.wml') !== false) || (strpos($accept, 'application/vnd.wap.xhtml+xml') !== false)): + $isMobile = true; + break; + case(isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])): + $isMobile = true; + break; + case(in_array(strtolower(substr($agent, 0, 4)), self::mobile_index_list())): + $isMobile = true; + break; + } - if(!headers_sent()) { - header('Cache-Control: no-transform'); - header('Vary: User-Agent, Accept'); - } + if (!headers_sent()) { + header('Cache-Control: no-transform'); + header('Vary: User-Agent, Accept'); + } - return $isMobile; - } + return $isMobile; + } - /** - * Rough detection of "tablet" user agents, based on their user agent string. - * - * CAUTION: Does NOT detect Windows 8 tablets, since there's no user-agent distinction between - * tablets and desktops in Windows 8. - * - * Loosely based off the (now discontinued) Categorizr library: - * http://www.brettjankord.com/2012/01/16/categorizr-a-modern-device-detection-script/ - * - * @param String User agent (defaults to $_SERVER) - * @return boolean - */ - public static function is_tablet($agent = null) { - if(!$agent) $agent = $_SERVER['HTTP_USER_AGENT']; + /** + * Rough detection of "tablet" user agents, based on their user agent string. + * + * CAUTION: Does NOT detect Windows 8 tablets, since there's no user-agent distinction between + * tablets and desktops in Windows 8. + * + * Loosely based off the (now discontinued) Categorizr library: + * http://www.brettjankord.com/2012/01/16/categorizr-a-modern-device-detection-script/ + * + * @param String User agent (defaults to $_SERVER) + * @return boolean + */ + public static function is_tablet($agent = null) + { + if (!$agent) { + $agent = $_SERVER['HTTP_USER_AGENT']; + } - // Check if user agent is a Tablet - if( - (preg_match('/iP(a|ro)d/i', $agent)) - || (preg_match('/tablet/i', $agent)) && (!preg_match('/RX-34/i', $agent)) || (preg_match('/FOLIO/i', $agent)) - ) { - return true; - } - // Check if user agent is an Android Tablet - else if ( - (preg_match('/Linux/i', $agent)) - && (preg_match('/Android/i', $agent)) - && (!preg_match('/Fennec|mobi|HTC.Magic|HTCX06HT|Nexus.One|SC-02B|fone.945/i', $agent)) - && (!preg_match('/Mobile/i', $agent)) - ) { - // see http://googlewebmastercentral.blogspot.de/2011/03/mo-better-to-also-detect-mobile-user.html - // see http://googlewebmastercentral.blogspot.de/2012/11/giving-tablet-users-full-sized-web.html - return true; - } - // Check if user agent is a Kindle or Kindle Fire - else if ( - (preg_match('/Kindle/i', $agent)) - || (preg_match('/Mac.OS/i', $agent)) - && (preg_match('/Silk/i', $agent)) - ) { - return true; - } - // Check if user agent is a pre Android 3.0 Tablet - else if ( - (preg_match('/GT-P10|SC-01C|SHW-M180S|SGH-T849|SCH-I800|SHW-M180L|SPH-P100|SGH-I987|zt180|HTC(.Flyer|\_Flyer)|Sprint.ATP51|ViewPad7|pandigital(sprnova|nova)|Ideos.S7|Dell.Streak.7|Advent.Vega|A101IT|A70BHT|MID7015|Next2|nook/i', $agent)) - || (preg_match('/MB511/i', $agent)) - && (preg_match('/RUTEM/i', $agent)) - ) { - return true; - } - // Browser is either a mobile handset or desktop. - else { - return false; - } - } + // Check if user agent is a Tablet + if ( + (preg_match('/iP(a|ro)d/i', $agent)) + || (preg_match('/tablet/i', $agent)) && (!preg_match('/RX-34/i', $agent)) || (preg_match('/FOLIO/i', $agent)) + ) { + return true; + } + // Check if user agent is an Android Tablet + elseif ( + (preg_match('/Linux/i', $agent)) + && (preg_match('/Android/i', $agent)) + && (!preg_match('/Fennec|mobi|HTC.Magic|HTCX06HT|Nexus.One|SC-02B|fone.945/i', $agent)) + && (!preg_match('/Mobile/i', $agent)) + ) { + // see http://googlewebmastercentral.blogspot.de/2011/03/mo-better-to-also-detect-mobile-user.html + // see http://googlewebmastercentral.blogspot.de/2012/11/giving-tablet-users-full-sized-web.html + return true; + } + // Check if user agent is a Kindle or Kindle Fire + elseif ( + (preg_match('/Kindle/i', $agent)) + || (preg_match('/Mac.OS/i', $agent)) + && (preg_match('/Silk/i', $agent)) + ) { + return true; + } + // Check if user agent is a pre Android 3.0 Tablet + elseif ( + (preg_match('/GT-P10|SC-01C|SHW-M180S|SGH-T849|SCH-I800|SHW-M180L|SPH-P100|SGH-I987|zt180|HTC(.Flyer|\_Flyer)|Sprint.ATP51|ViewPad7|pandigital(sprnova|nova)|Ideos.S7|Dell.Streak.7|Advent.Vega|A101IT|A70BHT|MID7015|Next2|nook/i', $agent)) + || (preg_match('/MB511/i', $agent)) + && (preg_match('/RUTEM/i', $agent)) + ) { + return true; + } + // Browser is either a mobile handset or desktop. + else { + return false; + } + } } diff --git a/code/MobileSiteConfigExtension.php b/code/MobileSiteConfigExtension.php index 391bfac..f189535 100644 --- a/code/MobileSiteConfigExtension.php +++ b/code/MobileSiteConfigExtension.php @@ -5,167 +5,191 @@ * * @package mobile */ -class MobileSiteConfigExtension extends DataExtension { - - /** - * The path the default mobile theme should be copied - * to when {@link SiteConfig} is first created in the database. - * - * @see MobileSiteConfigExtension::requireDefaultRecords() - * @var string - */ - protected static $theme_copy_path; - - public static $theme_copy_enabled = true; - - public static function set_theme_copy_path($path) { - self::$theme_copy_path = $path; - } - - public static function get_theme_copy_path() { - if(!self::$theme_copy_path) { - return '../' . THEMES_DIR . '/blackcandymobile'; - } else { - return self::$theme_copy_path; - } - } - - /** - * Extra statics variable to merge into {@link SiteConfig} - */ - private static $db = array( - // Comma-separated list of mobile domains, without protocol - 'MobileDomain' => 'Text', - // Comma-separated list of non-mobile domains, without protocol - 'FullSiteDomain' => 'Text', - 'MobileTheme' => 'Varchar(255)', - 'MobileSiteType' => 'Enum("Disabled,RedirectToDomain,MobileThemeOnly","Disabled")' - ); - - private static $defaults = array( - 'MobileTheme' => 'blackcandymobile', - 'MobileSiteType' => 'Disabled' - ); - - public function populateDefaults() { - parent::populateDefaults(); - - $this->owner->MobileDomain = 'm.' . $_SERVER['HTTP_HOST']; - $this->owner->FullSiteDomain = $_SERVER['HTTP_HOST']; - } - - /** - * @return String The first available domain, with the current protocol prefixed, - * suitable for redirections etc. - */ - public function getMobileDomainNormalized() { - $domains = explode(',', $this->owner->MobileDomain); - $domain = array_shift($domains); - if(!$domain) return false; - if(!parse_url($domain, PHP_URL_SCHEME)) $domain = Director::protocol() . $domain; - return $domain; - } - - /** - * @return String The first available domain, with the current protocol prefixed, - * suitable for redirections etc. - */ - public function getFullSiteDomainNormalized() { - $domains = explode(',', $this->owner->FullSiteDomain); - $domain = array_shift($domains); - if(!$domain) return false; - if(!parse_url($domain, PHP_URL_SCHEME)) $domain = Director::protocol() . $domain; - return $domain; - } - - /** - * Provide a default if MobileSiteType is not set. - * @return string - */ - public function getMobileSiteType() { - $defaults = $this->owner->stat('defaults'); - $value = $this->owner->getField('MobileSiteType'); - if(!$value) $value = $defaults['MobileSiteType']; - return $value; - } - - /** - * Return possible values for the MobileSiteType field mapped - * to a human readable title. - * @return array - */ - public function getMobileSiteTypes() { - $types = array(); - $types['Disabled'] = _t('MobileSiteConfig.DISABLED', 'Disabled'); - $types['RedirectToDomain'] = _t('MobileSiteConfig.REDIRECTDOMAIN', 'Mobile users are redirected to mobile domain'); - $types['MobileThemeOnly'] = _t('MobileSiteConfig.USEANOTHERTHEME', 'Mobile users see mobile theme, but no redirection occurs'); - return $types; - } - - /** - * The default theme is "blackcandymobile". If this is still set - * as a field on SiteConfig, then make sure that it's copied - * into the themes directory from the mobile module. - */ - public function augmentDatabase() { - $defaultThemes = array('blackcandymobile', 'jquerymobile'); - $currentTheme = $this->owner->getField('MobileTheme'); - if(!$currentTheme || in_array($currentTheme, $defaultThemes)) { - $this->copyDefaultTheme($currentTheme); - } - } - - /** - * @param String - */ - public static function copyDefaultTheme($theme = null) { - if(!self::$theme_copy_enabled) return; - if(!$theme) $theme = 'blackcandymobile'; - $src = BASE_PATH . '/' . MOBILE_DIR . '/themes/' . $theme; - if(!file_exists($src)) { - throw new InvalidArgumentException(sprintf('Theme "%s" not found in path %s', $theme, $src)); - } - - $dst = self::get_theme_copy_path(); - - if(!file_exists($dst)) { - @mkdir($dst); - if(is_writable($dst)) { - rcopy($src, $dst); - DB::alteration_message( - sprintf('Default mobile theme "%s" has been copied into the themes directory', $theme), - 'created' - ); - } else { - DB::alteration_message( - sprintf( - 'Could not copy default mobile theme "%s" into themes directory (permission denied). +class MobileSiteConfigExtension extends DataExtension +{ + + /** + * The path the default mobile theme should be copied + * to when {@link SiteConfig} is first created in the database. + * + * @see MobileSiteConfigExtension::requireDefaultRecords() + * @var string + */ + protected static $theme_copy_path; + + public static $theme_copy_enabled = true; + + public static function set_theme_copy_path($path) + { + self::$theme_copy_path = $path; + } + + public static function get_theme_copy_path() + { + if (!self::$theme_copy_path) { + return '../' . THEMES_DIR . '/blackcandymobile'; + } else { + return self::$theme_copy_path; + } + } + + /** + * Extra statics variable to merge into {@link SiteConfig} + */ + private static $db = array( + // Comma-separated list of mobile domains, without protocol + 'MobileDomain' => 'Text', + // Comma-separated list of non-mobile domains, without protocol + 'FullSiteDomain' => 'Text', + 'MobileTheme' => 'Varchar(255)', + 'MobileSiteType' => 'Enum("Disabled,RedirectToDomain,MobileThemeOnly","Disabled")' + ); + + private static $defaults = array( + 'MobileTheme' => 'blackcandymobile', + 'MobileSiteType' => 'Disabled' + ); + + public function populateDefaults() + { + parent::populateDefaults(); + + $this->owner->MobileDomain = 'm.' . $_SERVER['HTTP_HOST']; + $this->owner->FullSiteDomain = $_SERVER['HTTP_HOST']; + } + + /** + * @return String The first available domain, with the current protocol prefixed, + * suitable for redirections etc. + */ + public function getMobileDomainNormalized() + { + $domains = explode(',', $this->owner->MobileDomain); + $domain = array_shift($domains); + if (!$domain) { + return false; + } + if (!parse_url($domain, PHP_URL_SCHEME)) { + $domain = Director::protocol() . $domain; + } + return $domain; + } + + /** + * @return String The first available domain, with the current protocol prefixed, + * suitable for redirections etc. + */ + public function getFullSiteDomainNormalized() + { + $domains = explode(',', $this->owner->FullSiteDomain); + $domain = array_shift($domains); + if (!$domain) { + return false; + } + if (!parse_url($domain, PHP_URL_SCHEME)) { + $domain = Director::protocol() . $domain; + } + return $domain; + } + + /** + * Provide a default if MobileSiteType is not set. + * @return string + */ + public function getMobileSiteType() + { + $defaults = $this->owner->stat('defaults'); + $value = $this->owner->getField('MobileSiteType'); + if (!$value) { + $value = $defaults['MobileSiteType']; + } + return $value; + } + + /** + * Return possible values for the MobileSiteType field mapped + * to a human readable title. + * @return array + */ + public function getMobileSiteTypes() + { + $types = array(); + $types['Disabled'] = _t('MobileSiteConfig.DISABLED', 'Disabled'); + $types['RedirectToDomain'] = _t('MobileSiteConfig.REDIRECTDOMAIN', 'Mobile users are redirected to mobile domain'); + $types['MobileThemeOnly'] = _t('MobileSiteConfig.USEANOTHERTHEME', 'Mobile users see mobile theme, but no redirection occurs'); + return $types; + } + + /** + * The default theme is "blackcandymobile". If this is still set + * as a field on SiteConfig, then make sure that it's copied + * into the themes directory from the mobile module. + */ + public function augmentDatabase() + { + $defaultThemes = array('blackcandymobile', 'jquerymobile'); + $currentTheme = $this->owner->getField('MobileTheme'); + if (!$currentTheme || in_array($currentTheme, $defaultThemes)) { + $this->copyDefaultTheme($currentTheme); + } + } + + /** + * @param String + */ + public static function copyDefaultTheme($theme = null) + { + if (!self::$theme_copy_enabled) { + return; + } + if (!$theme) { + $theme = 'blackcandymobile'; + } + $src = BASE_PATH . '/' . MOBILE_DIR . '/themes/' . $theme; + if (!file_exists($src)) { + throw new InvalidArgumentException(sprintf('Theme "%s" not found in path %s', $theme, $src)); + } + + $dst = self::get_theme_copy_path(); + + if (!file_exists($dst)) { + @mkdir($dst); + if (is_writable($dst)) { + rcopy($src, $dst); + DB::alteration_message( + sprintf('Default mobile theme "%s" has been copied into the themes directory', $theme), + 'created' + ); + } else { + DB::alteration_message( + sprintf( + 'Could not copy default mobile theme "%s" into themes directory (permission denied). Please manually copy the "%s" directory from the mobile module into the themes directory.', - $theme, - $theme - ), - 'error' - ); - } - } - } - - /** - * Append extra fields to the new Mobile tab in the cms. - */ - public function updateCMSFields(FieldList $fields) { - $fields->addFieldsToTab( - 'Root.Mobile', - array( - OptionsetField::create('MobileSiteType', _t('MobileSiteConfig.MOBILESITEBEHAVIOUR', 'Mobile site behaviour'), $this->getMobileSiteTypes()), - TextField::create('MobileDomain', _t('MobileSiteConfig.MOBILEDOMAIN', 'Mobile domain (e.g. m.mysite.com, needs to be different from "Full site domain")')), - TextField::create('FullSiteDomain', _t('MobileSiteConfig.FULLSITEDOMAIN', 'Full site domain (e.g. mysite.com, usually doesn\'t need to be changed)')), - DropdownField::create('MobileTheme', _t('MobileSiteConfig.MOBILETHEME', 'Mobile theme'), $this->owner->getAvailableThemes()) - ->setEmptyString(_t('SiteConfig.DEFAULTTHEME', '(Use default theme)')) - ) - ); - } - + $theme, + $theme + ), + 'error' + ); + } + } + } + + /** + * Append extra fields to the new Mobile tab in the cms. + */ + public function updateCMSFields(FieldList $fields) + { + $fields->addFieldsToTab( + 'Root.Mobile', + array( + OptionsetField::create('MobileSiteType', _t('MobileSiteConfig.MOBILESITEBEHAVIOUR', 'Mobile site behaviour'), $this->getMobileSiteTypes()), + TextField::create('MobileDomain', _t('MobileSiteConfig.MOBILEDOMAIN', 'Mobile domain (e.g. m.mysite.com, needs to be different from "Full site domain")')), + TextField::create('FullSiteDomain', _t('MobileSiteConfig.FULLSITEDOMAIN', 'Full site domain (e.g. mysite.com, usually doesn\'t need to be changed)')), + DropdownField::create('MobileTheme', _t('MobileSiteConfig.MOBILETHEME', 'Mobile theme'), $this->owner->getAvailableThemes()) + ->setEmptyString(_t('SiteConfig.DEFAULTTHEME', '(Use default theme)')) + ) + ); + } } /** @@ -178,17 +202,18 @@ public function updateCMSFields(FieldList $fields) { * @param string $src Source path * @param string $dst Destination path */ -function rcopy($src, $dst) { - $dir = opendir($src); - @mkdir($dst); - while(false !== ($file = readdir($dir))) { - if(($file != '.') && ($file != '..') && ($file != '.svn')) { - if(is_dir($src . '/' . $file)) { - rcopy($src . '/' . $file, $dst . '/' . $file); - } else { - copy($src . '/' . $file, $dst . '/' . $file); - } - } - } - closedir($dir); +function rcopy($src, $dst) +{ + $dir = opendir($src); + @mkdir($dst); + while (false !== ($file = readdir($dir))) { + if (($file != '.') && ($file != '..') && ($file != '.svn')) { + if (is_dir($src . '/' . $file)) { + rcopy($src . '/' . $file, $dst . '/' . $file); + } else { + copy($src . '/' . $file, $dst . '/' . $file); + } + } + } + closedir($dir); } diff --git a/code/MobileSiteControllerExtension.php b/code/MobileSiteControllerExtension.php index 13a19b1..87d04f8 100644 --- a/code/MobileSiteControllerExtension.php +++ b/code/MobileSiteControllerExtension.php @@ -5,208 +5,226 @@ * * @package mobile */ -class MobileSiteControllerExtension extends Extension { - - /** - * The expiration time of a cookie set for full site requests - * from the mobile site. Default is 1 day - * @var int - */ - public static $cookie_expire_time = 1; - - /** - * Stores state information as to which site is currently served. - */ - private static $is_mobile = false; - - /** - * Override the default behavior to ensure that if this is a mobile device - * or if they are on the configured mobile domain then they receive the mobile site. - */ - public function onAfterInit() { - self::$is_mobile = false; - $config = SiteConfig::current_site_config(); - $request = $this->owner->getRequest(); - - // If we've accessed the homepage as /home/, then we redirect to / and don't want to double redirect here - if ($this->owner->redirectedTo()) { - return; - } - - // Enforce the site (cookie expires in 1 day) - $fullSite = $request->getVar('fullSite'); - - if(is_numeric($fullSite)) { - $fullSiteCookie = (int)$fullSite; - Cookie::set('fullSite', $fullSiteCookie); - - // use the host of the desktop version of the site to set cross-(sub)domain cookie - $domain = $config->FullSiteDomainNormalized; - - if (!empty($domain)) { - Cookie::set('fullSite', $fullSite, self::$cookie_expire_time, null, '.' . parse_url($domain, PHP_URL_HOST)); - } else { // otherwise just use a normal cookie with the default domain - Cookie::set('fullSite', $fullSite, self::$cookie_expire_time); - } - } - else { - $fullSiteCookie = Cookie::get('fullSite'); - } - - if(is_numeric($fullSiteCookie)) { - // Full site requested - if($fullSiteCookie) { - if($this->onMobileDomain() && $config->MobileSiteType == 'RedirectToDomain') { - return $this->owner->redirect($config->FullSiteDomainNormalized, 301); - } - - return; - } - // Mobile site requested - else { - if(!$this->onMobileDomain() && $config->MobileSiteType == 'RedirectToDomain') { - return $this->owner->redirect($config->MobileDomainNormalized, 301); - } - - Config::inst()->update('SSViewer', 'theme', $config->MobileTheme); - self::$is_mobile = true; - return; - } - } - - // If the user requested the mobile domain, set the right theme - if($this->onMobileDomain()) { - Config::inst()->update('SSViewer', 'theme', $config->MobileTheme); - self::$is_mobile = true; - } - - // User just wants to see a theme, but no redirect occurs - if(MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'MobileThemeOnly') { - Config::inst()->update('SSViewer', 'theme', $config->MobileTheme); - self::$is_mobile = true; - } - - // If on a mobile device, but not on the mobile domain and has been setup for redirection - if(!$this->onMobileDomain() && MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'RedirectToDomain') { - return $this->owner->redirect($config->MobileDomainNormalized, 301); - } - } - - /** - * Provide state information. We can't always rely on current theme, - * as the user may elect to use the same theme for both sites. - * - * Useful for example for template conditionals. - */ - static public function is_mobile() { - return self::$is_mobile; - } - - /** - * Return whether the user is requesting the mobile site - either by query string - * or by saved cookie. Falls back to browser detection for first time visitors - * - * @return boolean - */ - public function requestedMobileSite() { - $config = SiteConfig::current_site_config(); - if ($config->MobileSiteType == 'Disabled') return false; - - $request = $this->owner->getRequest(); - $fullSite = $request->getVar('fullSite'); - if (is_numeric($fullSite)) { - return ($fullSite == 0); - } - - $fullSiteCookie = Cookie::get('fullSite'); - if (is_numeric($fullSiteCookie)) { - return ($fullSiteCookie == 0); - } - - return MobileBrowserDetector::is_mobile(); - } - - /** - * Return whether the user is on the mobile version of the website. - * Caution: This only has an effect when "MobileSiteType" is configured as "RedirectToDomain". - * - * @return boolean - */ - public function onMobileDomain() { - $config = SiteConfig::current_site_config(); - $domains = explode(',', $config->MobileDomain); - foreach($domains as $domain) { - if(!parse_url($domain, PHP_URL_SCHEME)) $domain = Director::protocol() . $domain; // Normalize URL - $parts = parse_url($domain); - $compare = @$parts['host']; - if(@$parts['port']) $compare .= ':' . $parts['port']; - if($compare && $compare == $_SERVER['HTTP_HOST']) return true; - } - - return false; - } - - /** - * @return boolean - */ - public function isMobile() { - return MobileSiteControllerExtension::$is_mobile; - } - - /** - * Return a link to the full site. - * - * @return string - */ - public function FullSiteLink() { - return Controller::join_links($this->owner->Link(), '?fullSite=1'); - } - - /** - * @return string - */ - public function MobileSiteLink() { - return Controller::join_links($this->owner->Link(), '?fullSite=0'); - } - - /** - * Is the current HTTP_USER_AGENT a known iPhone or iPod Touch - * mobile agent string? - * - * @return boolean - */ - public function IsiPhone() { - return MobileBrowserDetector::is_iphone(); - } - - /** - * Is the current HTTP_USER_AGENT a known Android mobile - * agent string? - * - * @return boolean - */ - public function IsAndroid() { - return MobileBrowserDetector::is_android(); - } - - /** - * Is the current HTTP_USER_AGENT a known Opera Mini - * agent string? - * - * @return boolean - */ - public function IsOperaMini() { - return MobileBrowserDetector::is_opera_mini(); - } - - /** - * Is the current HTTP_USER_AGENT a known Blackberry - * mobile agent string? - * - * @return boolean - */ - public function IsBlackBerry() { - return MobileBrowserDetector::is_blackberry(); - } - +class MobileSiteControllerExtension extends Extension +{ + + /** + * The expiration time of a cookie set for full site requests + * from the mobile site. Default is 1 day + * @var int + */ + public static $cookie_expire_time = 1; + + /** + * Stores state information as to which site is currently served. + */ + private static $is_mobile = false; + + /** + * Override the default behavior to ensure that if this is a mobile device + * or if they are on the configured mobile domain then they receive the mobile site. + */ + public function onAfterInit() + { + self::$is_mobile = false; + $config = SiteConfig::current_site_config(); + $request = $this->owner->getRequest(); + + // If we've accessed the homepage as /home/, then we redirect to / and don't want to double redirect here + if ($this->owner->redirectedTo()) { + return; + } + + // Enforce the site (cookie expires in 1 day) + $fullSite = $request->getVar('fullSite'); + + if (is_numeric($fullSite)) { + $fullSiteCookie = (int)$fullSite; + Cookie::set('fullSite', $fullSiteCookie); + + // use the host of the desktop version of the site to set cross-(sub)domain cookie + $domain = $config->FullSiteDomainNormalized; + + if (!empty($domain)) { + Cookie::set('fullSite', $fullSite, self::$cookie_expire_time, null, '.' . parse_url($domain, PHP_URL_HOST)); + } else { // otherwise just use a normal cookie with the default domain + Cookie::set('fullSite', $fullSite, self::$cookie_expire_time); + } + } else { + $fullSiteCookie = Cookie::get('fullSite'); + } + + if (is_numeric($fullSiteCookie)) { + // Full site requested + if ($fullSiteCookie) { + if ($this->onMobileDomain() && $config->MobileSiteType == 'RedirectToDomain') { + return $this->owner->redirect($config->FullSiteDomainNormalized, 301); + } + + return; + } + // Mobile site requested + else { + if (!$this->onMobileDomain() && $config->MobileSiteType == 'RedirectToDomain') { + return $this->owner->redirect($config->MobileDomainNormalized, 301); + } + + Config::inst()->update('SSViewer', 'theme', $config->MobileTheme); + self::$is_mobile = true; + return; + } + } + + // If the user requested the mobile domain, set the right theme + if ($this->onMobileDomain()) { + Config::inst()->update('SSViewer', 'theme', $config->MobileTheme); + self::$is_mobile = true; + } + + // User just wants to see a theme, but no redirect occurs + if (MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'MobileThemeOnly') { + Config::inst()->update('SSViewer', 'theme', $config->MobileTheme); + self::$is_mobile = true; + } + + // If on a mobile device, but not on the mobile domain and has been setup for redirection + if (!$this->onMobileDomain() && MobileBrowserDetector::is_mobile() && $config->MobileSiteType == 'RedirectToDomain') { + return $this->owner->redirect($config->MobileDomainNormalized, 301); + } + } + + /** + * Provide state information. We can't always rely on current theme, + * as the user may elect to use the same theme for both sites. + * + * Useful for example for template conditionals. + */ + public static function is_mobile() + { + return self::$is_mobile; + } + + /** + * Return whether the user is requesting the mobile site - either by query string + * or by saved cookie. Falls back to browser detection for first time visitors + * + * @return boolean + */ + public function requestedMobileSite() + { + $config = SiteConfig::current_site_config(); + if ($config->MobileSiteType == 'Disabled') { + return false; + } + + $request = $this->owner->getRequest(); + $fullSite = $request->getVar('fullSite'); + if (is_numeric($fullSite)) { + return ($fullSite == 0); + } + + $fullSiteCookie = Cookie::get('fullSite'); + if (is_numeric($fullSiteCookie)) { + return ($fullSiteCookie == 0); + } + + return MobileBrowserDetector::is_mobile(); + } + + /** + * Return whether the user is on the mobile version of the website. + * Caution: This only has an effect when "MobileSiteType" is configured as "RedirectToDomain". + * + * @return boolean + */ + public function onMobileDomain() + { + $config = SiteConfig::current_site_config(); + $domains = explode(',', $config->MobileDomain); + foreach ($domains as $domain) { + if (!parse_url($domain, PHP_URL_SCHEME)) { + $domain = Director::protocol() . $domain; + } // Normalize URL + $parts = parse_url($domain); + $compare = @$parts['host']; + if (@$parts['port']) { + $compare .= ':' . $parts['port']; + } + if ($compare && $compare == $_SERVER['HTTP_HOST']) { + return true; + } + } + + return false; + } + + /** + * @return boolean + */ + public function isMobile() + { + return MobileSiteControllerExtension::$is_mobile; + } + + /** + * Return a link to the full site. + * + * @return string + */ + public function FullSiteLink() + { + return Controller::join_links($this->owner->Link(), '?fullSite=1'); + } + + /** + * @return string + */ + public function MobileSiteLink() + { + return Controller::join_links($this->owner->Link(), '?fullSite=0'); + } + + /** + * Is the current HTTP_USER_AGENT a known iPhone or iPod Touch + * mobile agent string? + * + * @return boolean + */ + public function IsiPhone() + { + return MobileBrowserDetector::is_iphone(); + } + + /** + * Is the current HTTP_USER_AGENT a known Android mobile + * agent string? + * + * @return boolean + */ + public function IsAndroid() + { + return MobileBrowserDetector::is_android(); + } + + /** + * Is the current HTTP_USER_AGENT a known Opera Mini + * agent string? + * + * @return boolean + */ + public function IsOperaMini() + { + return MobileBrowserDetector::is_opera_mini(); + } + + /** + * Is the current HTTP_USER_AGENT a known Blackberry + * mobile agent string? + * + * @return boolean + */ + public function IsBlackBerry() + { + return MobileBrowserDetector::is_blackberry(); + } } diff --git a/code/MobileSiteTreeExtension.php b/code/MobileSiteTreeExtension.php index 1c10775..2b94b37 100644 --- a/code/MobileSiteTreeExtension.php +++ b/code/MobileSiteTreeExtension.php @@ -2,19 +2,20 @@ /** * @package mobile */ -class MobileSiteTreeExtension extends DataExtension { - - function MetaTags(&$tags) { - $config = SiteConfig::current_site_config(); +class MobileSiteTreeExtension extends DataExtension +{ + + public function MetaTags(&$tags) + { + $config = SiteConfig::current_site_config(); - // Ensure a canonical link is placed, for semantic correctness and SEO - if(Controller::has_curr() && Controller::curr()->hasMethod("onMobileDomain") && Controller::curr()->onMobileDomain() && $config->MobileSiteType == 'RedirectToDomain') { - $oldBaseURL = Director::baseURL(); + // Ensure a canonical link is placed, for semantic correctness and SEO + if (Controller::has_curr() && Controller::curr()->hasMethod("onMobileDomain") && Controller::curr()->onMobileDomain() && $config->MobileSiteType == 'RedirectToDomain') { + $oldBaseURL = Director::baseURL(); - Config::inst()->update('Director', 'alternate_base_url', $config->FullSiteDomain); - $tags .= sprintf('', $this->owner->AbsoluteLink()) . "\n"; - Config::inst()->update('Director', 'alternate_base_url', $oldBaseURL); - } - } - + Config::inst()->update('Director', 'alternate_base_url', $config->FullSiteDomain); + $tags .= sprintf('', $this->owner->AbsoluteLink()) . "\n"; + Config::inst()->update('Director', 'alternate_base_url', $oldBaseURL); + } + } } diff --git a/tests/MobileBrowserDetectorTest.php b/tests/MobileBrowserDetectorTest.php index f73fc98..70c0463 100644 --- a/tests/MobileBrowserDetectorTest.php +++ b/tests/MobileBrowserDetectorTest.php @@ -1,19 +1,21 @@ assertTrue(MobileBrowserDetector::is_tablet( - 'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13', - 'XOOM tablet, contains "android" but not "mobile"' - )); - $this->assertFalse(MobileBrowserDetector::is_tablet( - 'Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1', - 'Nexus One, contains "mobile" and "android"' - )); - // This is where this approach falls down ... can't detect MS Surface usage. - // See http://www.brettjankord.com/2013/01/10/active-development-on-categorizr-has-come-to-an-end/ - $this->assertFalse(MobileBrowserDetector::is_tablet( - 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; ARM; Trident/6.0; Touch)' - )); - } -} \ No newline at end of file +class MobileBrowserDetectorTest extends SapphireTest +{ + + public function testIsTablet() + { + $this->assertTrue(MobileBrowserDetector::is_tablet( + 'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13', + 'XOOM tablet, contains "android" but not "mobile"' + )); + $this->assertFalse(MobileBrowserDetector::is_tablet( + 'Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1', + 'Nexus One, contains "mobile" and "android"' + )); + // This is where this approach falls down ... can't detect MS Surface usage. + // See http://www.brettjankord.com/2013/01/10/active-development-on-categorizr-has-come-to-an-end/ + $this->assertFalse(MobileBrowserDetector::is_tablet( + 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; ARM; Trident/6.0; Touch)' + )); + } +} diff --git a/tests/MobileSiteConfigExtensionTest.php b/tests/MobileSiteConfigExtensionTest.php index 9e1fee1..932943f 100644 --- a/tests/MobileSiteConfigExtensionTest.php +++ b/tests/MobileSiteConfigExtensionTest.php @@ -3,45 +3,51 @@ * @package mobile * @subpackage tests */ -class MobileSiteConfigExtensionTest extends SapphireTest { - - public function setUp() { - MobileSiteConfigExtension::set_theme_copy_path(TEMP_FOLDER . '/mobile-test-copy-theme/'); - MobileSiteConfigExtension::copyDefaultTheme(); - - parent::setUp(); - } - - public function testRequireDefaultRecordsCopiesDefaultThemeWhenDefaultThemeSet() { - $config = SiteConfig::current_site_config(); - $config->MobileTheme = 'blackcandymobile'; - $config->write(); - - $this->assertTrue(file_exists(TEMP_FOLDER . '/mobile-test-copy-theme/')); - } - - public function testMobileSiteTypesField() { - $config = SiteConfig::current_site_config(); - $fields = $config->getCMSFields(); - $typeField = $fields->dataFieldByName('MobileSiteType'); - $this->assertEquals($typeField->getSource(), $config->getMobileSiteTypes()); - } - - public function testMobileDomainGetterAddsProtocolPrefix() { - $config = SiteConfig::current_site_config(); - $config->MobileDomain = 'mobile.mysite.com'; - $this->assertEquals('http://mobile.mysite.com', $config->MobileDomainNormalized); - } - - public function testFullSiteDomainGetterAddsProtocolPrefix() { - $config = SiteConfig::current_site_config(); - $config->FullSiteDomain = 'mysite.com'; - $this->assertEquals('http://mysite.com', $config->FullSiteDomainNormalized); - } - - public function tearDown() { - parent::tearDown(); - exec('rm -rf ' . TEMP_FOLDER . '/mobile-test-copy-theme/'); - } - +class MobileSiteConfigExtensionTest extends SapphireTest +{ + + public function setUp() + { + MobileSiteConfigExtension::set_theme_copy_path(TEMP_FOLDER . '/mobile-test-copy-theme/'); + MobileSiteConfigExtension::copyDefaultTheme(); + + parent::setUp(); + } + + public function testRequireDefaultRecordsCopiesDefaultThemeWhenDefaultThemeSet() + { + $config = SiteConfig::current_site_config(); + $config->MobileTheme = 'blackcandymobile'; + $config->write(); + + $this->assertTrue(file_exists(TEMP_FOLDER . '/mobile-test-copy-theme/')); + } + + public function testMobileSiteTypesField() + { + $config = SiteConfig::current_site_config(); + $fields = $config->getCMSFields(); + $typeField = $fields->dataFieldByName('MobileSiteType'); + $this->assertEquals($typeField->getSource(), $config->getMobileSiteTypes()); + } + + public function testMobileDomainGetterAddsProtocolPrefix() + { + $config = SiteConfig::current_site_config(); + $config->MobileDomain = 'mobile.mysite.com'; + $this->assertEquals('http://mobile.mysite.com', $config->MobileDomainNormalized); + } + + public function testFullSiteDomainGetterAddsProtocolPrefix() + { + $config = SiteConfig::current_site_config(); + $config->FullSiteDomain = 'mysite.com'; + $this->assertEquals('http://mysite.com', $config->FullSiteDomainNormalized); + } + + public function tearDown() + { + parent::tearDown(); + exec('rm -rf ' . TEMP_FOLDER . '/mobile-test-copy-theme/'); + } } diff --git a/tests/MobileSiteControllerExtensionTest.php b/tests/MobileSiteControllerExtensionTest.php index 8a36b84..fe0f26b 100644 --- a/tests/MobileSiteControllerExtensionTest.php +++ b/tests/MobileSiteControllerExtensionTest.php @@ -3,256 +3,278 @@ * @package mobile * @subpackage tests */ -class MobileSiteControllerExtensionTest extends FunctionalTest { - - public static $fixture_file = 'mobile/tests/MobileSiteControllerExtensionTest.yml'; - - public static $use_draft_site = true; - - protected static $originalUserAgent; - - protected static $originalHost; - - protected static $originalAccept; - - protected $autoFollowRedirection = false; - - public function setUp() { - parent::setUp(); - if(!self::$originalUserAgent) self::$originalUserAgent = $_SERVER['HTTP_USER_AGENT']; - if(!self::$originalHost) self::$originalHost = $_SERVER['HTTP_HOST']; - if(!self::$originalAccept) self::$originalAccept = $_SERVER['HTTP_ACCEPT']; - } - - public function tearDown() { - parent::tearDown(); - $_SERVER['HTTP_USER_AGENT'] = self::$originalUserAgent; - $_SERVER['HTTP_HOST'] = self::$originalHost; - $_SERVER['HTTP_ACCEPT'] = self::$originalAccept; - } - - public function testFullSiteLink() { - $page = $this->objFromFixture('Page', 'page'); - $controller = new ContentController($page); - $this->assertTrue(strpos($controller->FullSiteLink(), 'page/?fullSite=1') !== false); - } - - public function testRedirectToMobileSite() { - $page = $this->objFromFixture('Page', 'page'); - $config = SiteConfig::current_site_config(); - $config->MobileSiteType = 'RedirectToDomain'; - $config->write(); - - // Test common mobile agent strings - foreach(array( - 'Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; de-de) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20', - 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16', - 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3', - 'Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; ADR6300 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17', - 'iPod', - 'iPhone', - 'Android', - 'palm os', - 'hiptop', - 'avantgo', - 'iemobile', - '502i', - '3gso', - '6310', - 'windows ce; ppc;', - 'Stuff; iPhone; Version details here', - 'Stuff here; Android; Version details here', - 'Opera/9.50 (J2ME/MIDP; Opera Mini/4.0.10031/298; U; en)', - 'Opera/8.0.1 (J2ME/MIDP; Opera Mini/3.1.9427/1724; en; U; ssr)', - 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+' - ) as $agent) { - $_SERVER['HTTP_USER_AGENT'] = $agent; - $response = $this->get($page->URLSegment); - $headers = $response->getHeaders(); - $this->assertEquals(301, $response->getStatusCode()); - $this->assertEquals('http://m.' . $_SERVER['HTTP_HOST'], $headers['Location']); - } - } - - public function testRedirectToMobileFromAcceptSetting() { - $page = $this->objFromFixture('Page', 'page'); - $config = SiteConfig::current_site_config(); - $config->MobileSiteType = 'RedirectToDomain'; - $config->write(); - - $_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml'; - $response = $this->get($page->URLSegment); - $headers = $response->getHeaders(); - $this->assertEquals(301, $response->getStatusCode()); - $this->assertEquals('http://m.' . $_SERVER['HTTP_HOST'], $headers['Location']); - } - - public function testRedirectToMobileFromWAPProfile() { - $page = $this->objFromFixture('Page', 'page'); - $config = SiteConfig::current_site_config(); - $config->MobileSiteType = 'RedirectToDomain'; - $config->write(); - - $_SERVER['HTTP_X_WAP_PROFILE'] = 1; - $response = $this->get($page->URLSegment); - $headers = $response->getHeaders(); - $this->assertEquals(301, $response->getStatusCode()); - $this->assertEquals('http://m.' . $_SERVER['HTTP_HOST'], $headers['Location']); - unset($_SERVER['HTTP_X_WAP_PROFILE']); - } - - public function testNoRedirectToMobile() { - $config = SiteConfig::current_site_config(); - $config->MobileSiteType = 'MobileThemeOnly'; - $config->write(); - $page = $this->objFromFixture('Page', 'page'); - $_SERVER['HTTP_USER_AGENT'] = 'iPhone'; - $response = $this->get($page->URLSegment); - $headers = $response->getHeaders(); - $this->assertEquals(200, $response->getStatusCode()); - } - - public function testRedirectToFullSiteFromMobile() { - $config = SiteConfig::current_site_config(); - $config->MobileSiteType = 'RedirectToDomain'; - $config->write(); - $page = $this->objFromFixture('Page', 'page'); - $_SERVER['HTTP_HOST'] = 'm.' . $_SERVER['HTTP_HOST']; - $response = $this->get($page->URLSegment . '?fullSite=1', null, null, array('fullSite' => 1)); - $headers = $response->getHeaders(); - $this->assertEquals(301, $response->getStatusCode()); - $this->assertEquals($config->FullSiteDomainNormalized, $headers['Location']); - } - - public function testRedirectToMobileSiteFromDesktop() { - $config = SiteConfig::current_site_config(); - $config->MobileSiteType = 'RedirectToDomain'; - $config->write(); - $page = $this->objFromFixture('Page', 'page'); - $response = $this->get($page->URLSegment . '?fullSite=0', null, null, array('fullSite' => 0)); - $headers = $response->getHeaders(); - $this->assertEquals(301, $response->getStatusCode()); - $this->assertEquals($config->MobileDomainNormalized, $headers['Location']); - } - - public function testNoMobileRedirectWhenFullSiteSessionSetOnMobile() { - $_SERVER['HTTP_USER_AGENT'] = 'Android'; - $config = SiteConfig::current_site_config(); - $config->MobileSiteType = 'RedirectToDomain'; - $config->write(); - $page = $this->objFromFixture('Page', 'page'); - $response = $this->get($page->URLSegment, null, null, array('fullSite' => 1)); - $headers = $response->getHeaders(); - $this->assertEquals(200, $response->getStatusCode()); - } - - public function testOnMobileSite() { - $_SERVER['HTTP_HOST'] = str_replace('m.', '', $_SERVER['HTTP_HOST']); - $controller = new ContentController(); - $this->assertFalse($controller->onMobileDomain()); - $_SERVER['HTTP_HOST'] = 'm.' . $_SERVER['HTTP_HOST']; - $this->assertTrue($controller->onMobileDomain()); - } - - public function testOnMobileSiteMultipleDomains() { - $config = SiteConfig::current_site_config(); - $config->FullSiteDomain = 'domain1.com,domain2.com'; - $config->MobileDomain = 'm.domain1.com,m.domain2.com'; - $config->write(); - $_SERVER['HTTP_HOST'] = 'domain1.com'; - $controller = new ContentController(); - $this->assertFalse($controller->onMobileDomain()); - $_SERVER['HTTP_HOST'] = 'm.domain1.com'; - $this->assertTrue($controller->onMobileDomain()); - $_SERVER['HTTP_HOST'] = 'm.domain2.com'; - $this->assertTrue($controller->onMobileDomain()); - } - - public function testDirectlyAccessingMobileSiteOnAnyDevice() { - $_SERVER['HTTP_USER_AGENT'] = 'anything can be here'; - $_SERVER['HTTP_HOST'] = 'm.' . $_SERVER['HTTP_HOST']; - $page = $this->objFromFixture('Page', 'page'); - $response = $this->get($page->URLSegment); - $headers = $response->getHeaders(); - $this->assertEquals(200, $response->getStatusCode()); - } - - public function testIsiPhone() { - $controller = new ContentController(); - $_SERVER['HTTP_USER_AGENT'] = 'something else'; - $this->assertFalse($controller->IsiPhone()); - $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; de-de) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20'; - $this->assertTrue($controller->IsiPhone()); - $_SERVER['HTTP_USER_AGENT'] = 'Something here; iPhone; Probably something else here'; - $this->assertTrue($controller->IsiPhone()); - } - - public function testIsAndroid() { - $controller = new ContentController(); - $_SERVER['HTTP_USER_AGENT'] = 'something else'; - $this->assertFalse($controller->IsAndroid()); - $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; ADR6300 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17'; - $this->assertTrue($controller->IsAndroid()); - $_SERVER['HTTP_USER_AGENT'] = 'Something here; Android; Probably something else here'; - $this->assertTrue($controller->IsAndroid()); - } - - public function testIsOperaMini() { - $controller = new ContentController(); - $_SERVER['HTTP_USER_AGENT'] = 'something else'; - $this->assertFalse($controller->IsOperaMini()); - $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.50 (J2ME/MIDP; Opera Mini/4.0.10031/298; U; en)'; - $this->assertTrue($controller->IsOperaMini()); - $_SERVER['HTTP_USER_AGENT'] = 'Something here; Opera Mini; Probably something else here'; - $this->assertTrue($controller->IsOperaMini()); - } - - public function testIsBlackBerry() { - $controller = new ContentController(); - $_SERVER['HTTP_USER_AGENT'] = 'something else'; - $this->assertFalse($controller->IsBlackBerry()); - $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+'; - $this->assertTrue($controller->IsBlackBerry()); - $_SERVER['HTTP_USER_AGENT'] = 'Something here; BlackBerry; Probably something else here'; - $this->assertTrue($controller->IsBlackBerry()); - } - - public function testIsMobile() { - $page = $this->objFromFixture('Page', 'page'); - $config = SiteConfig::current_site_config(); - - // Immobile device - $_SERVER['HTTP_USER_AGENT'] = 'immobile browser'; - $response = $this->get($page->URLSegment); - $this->assertFalse(MobileSiteControllerExtension::is_mobile()); - - // Force mobile site - $_SERVER['HTTP_USER_AGENT'] = 'immobile browser'; - $response = $this->get($page->URLSegment.'?fullSite=0'); - $this->assertTrue(MobileSiteControllerExtension::is_mobile()); - - $config->MobileSiteType = 'MobileThemeOnly'; - $config->write(); - // Mobile device - $_SERVER['HTTP_USER_AGENT'] = 'iPhone'; - $response = $this->get($page->URLSegment); - $this->assertTrue(MobileSiteControllerExtension::is_mobile()); - - // Force full site - $_SERVER['HTTP_USER_AGENT'] = 'iPhone'; - $response = $this->get($page->URLSegment.'?fullSite=1'); - $this->assertFalse(MobileSiteControllerExtension::is_mobile()); - - $config->MobileSiteType = 'RedirectToDomain'; - $config->write(); - // Mobile domain - $_SERVER['HTTP_USER_AGENT'] = 'anything can be here'; - $_SERVER['HTTP_HOST'] = 'm.' . $_SERVER['HTTP_HOST']; - $page = $this->objFromFixture('Page', 'page'); - $response = $this->get($page->URLSegment); - $this->assertTrue(MobileSiteControllerExtension::is_mobile()); - - - } - +class MobileSiteControllerExtensionTest extends FunctionalTest +{ + + public static $fixture_file = 'mobile/tests/MobileSiteControllerExtensionTest.yml'; + + public static $use_draft_site = true; + + protected static $originalUserAgent; + + protected static $originalHost; + + protected static $originalAccept; + + protected $autoFollowRedirection = false; + + public function setUp() + { + parent::setUp(); + if (!self::$originalUserAgent) { + self::$originalUserAgent = $_SERVER['HTTP_USER_AGENT']; + } + if (!self::$originalHost) { + self::$originalHost = $_SERVER['HTTP_HOST']; + } + if (!self::$originalAccept) { + self::$originalAccept = $_SERVER['HTTP_ACCEPT']; + } + } + + public function tearDown() + { + parent::tearDown(); + $_SERVER['HTTP_USER_AGENT'] = self::$originalUserAgent; + $_SERVER['HTTP_HOST'] = self::$originalHost; + $_SERVER['HTTP_ACCEPT'] = self::$originalAccept; + } + + public function testFullSiteLink() + { + $page = $this->objFromFixture('Page', 'page'); + $controller = new ContentController($page); + $this->assertTrue(strpos($controller->FullSiteLink(), 'page/?fullSite=1') !== false); + } + + public function testRedirectToMobileSite() + { + $page = $this->objFromFixture('Page', 'page'); + $config = SiteConfig::current_site_config(); + $config->MobileSiteType = 'RedirectToDomain'; + $config->write(); + + // Test common mobile agent strings + foreach (array( + 'Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; de-de) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20', + 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16', + 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3', + 'Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; ADR6300 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17', + 'iPod', + 'iPhone', + 'Android', + 'palm os', + 'hiptop', + 'avantgo', + 'iemobile', + '502i', + '3gso', + '6310', + 'windows ce; ppc;', + 'Stuff; iPhone; Version details here', + 'Stuff here; Android; Version details here', + 'Opera/9.50 (J2ME/MIDP; Opera Mini/4.0.10031/298; U; en)', + 'Opera/8.0.1 (J2ME/MIDP; Opera Mini/3.1.9427/1724; en; U; ssr)', + 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+' + ) as $agent) { + $_SERVER['HTTP_USER_AGENT'] = $agent; + $response = $this->get($page->URLSegment); + $headers = $response->getHeaders(); + $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals('http://m.' . $_SERVER['HTTP_HOST'], $headers['Location']); + } + } + + public function testRedirectToMobileFromAcceptSetting() + { + $page = $this->objFromFixture('Page', 'page'); + $config = SiteConfig::current_site_config(); + $config->MobileSiteType = 'RedirectToDomain'; + $config->write(); + + $_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml'; + $response = $this->get($page->URLSegment); + $headers = $response->getHeaders(); + $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals('http://m.' . $_SERVER['HTTP_HOST'], $headers['Location']); + } + + public function testRedirectToMobileFromWAPProfile() + { + $page = $this->objFromFixture('Page', 'page'); + $config = SiteConfig::current_site_config(); + $config->MobileSiteType = 'RedirectToDomain'; + $config->write(); + + $_SERVER['HTTP_X_WAP_PROFILE'] = 1; + $response = $this->get($page->URLSegment); + $headers = $response->getHeaders(); + $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals('http://m.' . $_SERVER['HTTP_HOST'], $headers['Location']); + unset($_SERVER['HTTP_X_WAP_PROFILE']); + } + + public function testNoRedirectToMobile() + { + $config = SiteConfig::current_site_config(); + $config->MobileSiteType = 'MobileThemeOnly'; + $config->write(); + $page = $this->objFromFixture('Page', 'page'); + $_SERVER['HTTP_USER_AGENT'] = 'iPhone'; + $response = $this->get($page->URLSegment); + $headers = $response->getHeaders(); + $this->assertEquals(200, $response->getStatusCode()); + } + + public function testRedirectToFullSiteFromMobile() + { + $config = SiteConfig::current_site_config(); + $config->MobileSiteType = 'RedirectToDomain'; + $config->write(); + $page = $this->objFromFixture('Page', 'page'); + $_SERVER['HTTP_HOST'] = 'm.' . $_SERVER['HTTP_HOST']; + $response = $this->get($page->URLSegment . '?fullSite=1', null, null, array('fullSite' => 1)); + $headers = $response->getHeaders(); + $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals($config->FullSiteDomainNormalized, $headers['Location']); + } + + public function testRedirectToMobileSiteFromDesktop() + { + $config = SiteConfig::current_site_config(); + $config->MobileSiteType = 'RedirectToDomain'; + $config->write(); + $page = $this->objFromFixture('Page', 'page'); + $response = $this->get($page->URLSegment . '?fullSite=0', null, null, array('fullSite' => 0)); + $headers = $response->getHeaders(); + $this->assertEquals(301, $response->getStatusCode()); + $this->assertEquals($config->MobileDomainNormalized, $headers['Location']); + } + + public function testNoMobileRedirectWhenFullSiteSessionSetOnMobile() + { + $_SERVER['HTTP_USER_AGENT'] = 'Android'; + $config = SiteConfig::current_site_config(); + $config->MobileSiteType = 'RedirectToDomain'; + $config->write(); + $page = $this->objFromFixture('Page', 'page'); + $response = $this->get($page->URLSegment, null, null, array('fullSite' => 1)); + $headers = $response->getHeaders(); + $this->assertEquals(200, $response->getStatusCode()); + } + + public function testOnMobileSite() + { + $_SERVER['HTTP_HOST'] = str_replace('m.', '', $_SERVER['HTTP_HOST']); + $controller = new ContentController(); + $this->assertFalse($controller->onMobileDomain()); + $_SERVER['HTTP_HOST'] = 'm.' . $_SERVER['HTTP_HOST']; + $this->assertTrue($controller->onMobileDomain()); + } + + public function testOnMobileSiteMultipleDomains() + { + $config = SiteConfig::current_site_config(); + $config->FullSiteDomain = 'domain1.com,domain2.com'; + $config->MobileDomain = 'm.domain1.com,m.domain2.com'; + $config->write(); + $_SERVER['HTTP_HOST'] = 'domain1.com'; + $controller = new ContentController(); + $this->assertFalse($controller->onMobileDomain()); + $_SERVER['HTTP_HOST'] = 'm.domain1.com'; + $this->assertTrue($controller->onMobileDomain()); + $_SERVER['HTTP_HOST'] = 'm.domain2.com'; + $this->assertTrue($controller->onMobileDomain()); + } + + public function testDirectlyAccessingMobileSiteOnAnyDevice() + { + $_SERVER['HTTP_USER_AGENT'] = 'anything can be here'; + $_SERVER['HTTP_HOST'] = 'm.' . $_SERVER['HTTP_HOST']; + $page = $this->objFromFixture('Page', 'page'); + $response = $this->get($page->URLSegment); + $headers = $response->getHeaders(); + $this->assertEquals(200, $response->getStatusCode()); + } + + public function testIsiPhone() + { + $controller = new ContentController(); + $_SERVER['HTTP_USER_AGENT'] = 'something else'; + $this->assertFalse($controller->IsiPhone()); + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 2_0 like Mac OS X; de-de) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5A347 Safari/525.20'; + $this->assertTrue($controller->IsiPhone()); + $_SERVER['HTTP_USER_AGENT'] = 'Something here; iPhone; Probably something else here'; + $this->assertTrue($controller->IsiPhone()); + } + + public function testIsAndroid() + { + $controller = new ContentController(); + $_SERVER['HTTP_USER_AGENT'] = 'something else'; + $this->assertFalse($controller->IsAndroid()); + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; ADR6300 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17'; + $this->assertTrue($controller->IsAndroid()); + $_SERVER['HTTP_USER_AGENT'] = 'Something here; Android; Probably something else here'; + $this->assertTrue($controller->IsAndroid()); + } + + public function testIsOperaMini() + { + $controller = new ContentController(); + $_SERVER['HTTP_USER_AGENT'] = 'something else'; + $this->assertFalse($controller->IsOperaMini()); + $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.50 (J2ME/MIDP; Opera Mini/4.0.10031/298; U; en)'; + $this->assertTrue($controller->IsOperaMini()); + $_SERVER['HTTP_USER_AGENT'] = 'Something here; Opera Mini; Probably something else here'; + $this->assertTrue($controller->IsOperaMini()); + } + + public function testIsBlackBerry() + { + $controller = new ContentController(); + $_SERVER['HTTP_USER_AGENT'] = 'something else'; + $this->assertFalse($controller->IsBlackBerry()); + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+'; + $this->assertTrue($controller->IsBlackBerry()); + $_SERVER['HTTP_USER_AGENT'] = 'Something here; BlackBerry; Probably something else here'; + $this->assertTrue($controller->IsBlackBerry()); + } + + public function testIsMobile() + { + $page = $this->objFromFixture('Page', 'page'); + $config = SiteConfig::current_site_config(); + + // Immobile device + $_SERVER['HTTP_USER_AGENT'] = 'immobile browser'; + $response = $this->get($page->URLSegment); + $this->assertFalse(MobileSiteControllerExtension::is_mobile()); + + // Force mobile site + $_SERVER['HTTP_USER_AGENT'] = 'immobile browser'; + $response = $this->get($page->URLSegment.'?fullSite=0'); + $this->assertTrue(MobileSiteControllerExtension::is_mobile()); + + $config->MobileSiteType = 'MobileThemeOnly'; + $config->write(); + // Mobile device + $_SERVER['HTTP_USER_AGENT'] = 'iPhone'; + $response = $this->get($page->URLSegment); + $this->assertTrue(MobileSiteControllerExtension::is_mobile()); + + // Force full site + $_SERVER['HTTP_USER_AGENT'] = 'iPhone'; + $response = $this->get($page->URLSegment.'?fullSite=1'); + $this->assertFalse(MobileSiteControllerExtension::is_mobile()); + + $config->MobileSiteType = 'RedirectToDomain'; + $config->write(); + // Mobile domain + $_SERVER['HTTP_USER_AGENT'] = 'anything can be here'; + $_SERVER['HTTP_HOST'] = 'm.' . $_SERVER['HTTP_HOST']; + $page = $this->objFromFixture('Page', 'page'); + $response = $this->get($page->URLSegment); + $this->assertTrue(MobileSiteControllerExtension::is_mobile()); + } } diff --git a/tests/MobileSiteTreeExtensionTest.php b/tests/MobileSiteTreeExtensionTest.php index 75437f1..5744eb1 100644 --- a/tests/MobileSiteTreeExtensionTest.php +++ b/tests/MobileSiteTreeExtensionTest.php @@ -3,47 +3,49 @@ * @package mobile * @subpackage tests */ -class MobileSiteTreeExtensionTest extends FunctionalTest { - - public static $fixture_file = 'mobile/tests/MobileSiteControllerExtensionTest.yml'; +class MobileSiteTreeExtensionTest extends FunctionalTest +{ + + public static $fixture_file = 'mobile/tests/MobileSiteControllerExtensionTest.yml'; - public static $use_draft_site = true; + public static $use_draft_site = true; - protected $autoFollowRedirection = false; - - protected $requiredExtensions = array( - 'SiteTree' => array('MobileSiteTreeExtension'), - 'SiteConfig' => array('MobileSiteConfigExtension'), - ); + protected $autoFollowRedirection = false; + + protected $requiredExtensions = array( + 'SiteTree' => array('MobileSiteTreeExtension'), + 'SiteConfig' => array('MobileSiteConfigExtension'), + ); - public function setUp() { - parent::setUp(); - MobileSiteConfigExtension::set_theme_copy_path(TEMP_FOLDER . '/mobile-test-copy-theme/'); - } + public function setUp() + { + parent::setUp(); + MobileSiteConfigExtension::set_theme_copy_path(TEMP_FOLDER . '/mobile-test-copy-theme/'); + } - public function testShowsCanonicalLinkOnMobile() { - $page = $this->objFromFixture('Page', 'page'); - - $config = SiteConfig::current_site_config(); - $config->MobileDomain = 'http://m.test.com'; - $config->FullSiteDomain = 'http://test.com'; - $config->MobileSiteType = 'RedirectToDomain'; - $config->write(); - - $origHost = $_SERVER['HTTP_HOST']; - - $_SERVER['HTTP_HOST'] = 'test.com'; - $response = $this->get($page->RelativeLink()); - $canonicalEls = $this->cssParser()->getByXpath('//link[@rel=\'canonical\']'); - $this->assertTrue(empty($canonicalEls), 'Canonical links not included on desktp'); + public function testShowsCanonicalLinkOnMobile() + { + $page = $this->objFromFixture('Page', 'page'); + + $config = SiteConfig::current_site_config(); + $config->MobileDomain = 'http://m.test.com'; + $config->FullSiteDomain = 'http://test.com'; + $config->MobileSiteType = 'RedirectToDomain'; + $config->write(); + + $origHost = $_SERVER['HTTP_HOST']; + + $_SERVER['HTTP_HOST'] = 'test.com'; + $response = $this->get($page->RelativeLink()); + $canonicalEls = $this->cssParser()->getByXpath('//link[@rel=\'canonical\']'); + $this->assertTrue(empty($canonicalEls), 'Canonical links not included on desktp'); - $_SERVER['HTTP_HOST'] = 'm.test.com'; - $response = $this->get($page->RelativeLink()); - $canonicalEls = $this->cssParser()->getByXpath('//link[@rel=\'canonical\']'); - $this->assertFalse(empty($canonicalEls), 'Canonical links included on mobile'); - $this->assertEquals('http://test.com/page/', (string)$canonicalEls[0]['href'], 'Canonical link matches correct page'); - - $_SERVER['HTTP_HOST'] = $origHost; - } - -} \ No newline at end of file + $_SERVER['HTTP_HOST'] = 'm.test.com'; + $response = $this->get($page->RelativeLink()); + $canonicalEls = $this->cssParser()->getByXpath('//link[@rel=\'canonical\']'); + $this->assertFalse(empty($canonicalEls), 'Canonical links included on mobile'); + $this->assertEquals('http://test.com/page/', (string)$canonicalEls[0]['href'], 'Canonical link matches correct page'); + + $_SERVER['HTTP_HOST'] = $origHost; + } +}