Просмотр исходного кода

ZF-7794: Navigation with scheme classification

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24964 44c647ce-9c0f-0410-b52a-842ac1e357ba
adamlundrigan 13 лет назад
Родитель
Сommit
2693af68f7

+ 76 - 1
library/Zend/Navigation/Page/Mvc.php

@@ -110,6 +110,14 @@ class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
     protected $_active = null;
 
     /**
+     * Scheme to use when assembling URL
+     *
+     * @see getHref()
+     * @var string
+     */
+    protected $_scheme;
+
+    /**
      * Cached href
      *
      * The use of this variable minimizes execution time when getHref() is
@@ -128,6 +136,14 @@ class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
      */
     protected static $_urlHelper = null;
 
+    /**
+     * View helper for assembling URLs with schemes
+     *
+     * @see getHref()
+     * @var Zend_View_Helper_ServerUrl
+     */
+    protected static $_schemeHelper = null;
+
     // Accessors:
 
     /**
@@ -237,6 +253,17 @@ class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
                                       $this->getResetParams(),
                                       $this->getEncodeUrl());
 
+        // Use scheme?
+        $scheme = $this->getScheme();
+        if (null !== $scheme) {
+            if (null === self::$_schemeHelper) {
+                require_once 'Zend/View/Helper/ServerUrl.php';
+                self::$_schemeHelper = new Zend_View_Helper_ServerUrl();
+            }
+
+            $url = self::$_schemeHelper->setScheme($scheme)->serverUrl($url);
+        }
+
         // Add the fragment identifier if it is set
         $fragment = $this->getFragment();       
         if (null !== $fragment) {
@@ -569,6 +596,39 @@ class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
     }
 
     /**
+     * Sets scheme to use when assembling URL
+     *
+     * @see getHref()
+     *
+     * @param  string|null $scheme        scheme
+     * @return Zend_Navigation_Page_Mvc   fluent interface, returns self
+     */
+    public function setScheme($scheme)
+    {
+        if (null !== $scheme && !is_string($scheme)) {
+            require_once 'Zend/Navigation/Exception.php';
+            throw new Zend_Navigation_Exception(
+                'Invalid argument: $scheme must be a string or null'
+            );
+        }
+
+        $this->_scheme = $scheme;
+        return $this;
+    }
+
+    /**
+     * Returns scheme to use when assembling URL
+     *
+     * @see getHref()
+     *
+     * @return string|null  scheme or null
+     */
+    public function getScheme()
+    {
+        return $this->_scheme;
+    }
+
+    /**
      * Sets action helper for assembling URLs
      *
      * @see getHref()
@@ -581,6 +641,19 @@ class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
         self::$_urlHelper = $uh;
     }
 
+    /**
+     * Sets view helper for assembling URLs with schemes
+     *
+     * @see getHref()
+     *
+     * @param  Zend_View_Helper_ServerUrl $sh   scheme helper
+     * @return void
+     */
+    public static function setSchemeHelper(Zend_View_Helper_ServerUrl $sh)
+    {
+        self::$_schemeHelper = $sh;
+    }
+
     // Public methods:
 
     /**
@@ -600,6 +673,8 @@ class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
                 'route'        => $this->getRoute(),
                 'reset_params' => $this->getResetParams(),
                 'encodeUrl'    => $this->getEncodeUrl(),
-            ));
+                'scheme'       => $this->getScheme(),
+            )
+        );
     }
 }

+ 129 - 21
tests/Zend/Navigation/Page/MvcTest.php

@@ -559,25 +559,105 @@ class Zend_Navigation_Page_MvcTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('/pagexy/subpage.html', $page->getHref());
     }
 
+    /**
+     * @group ZF-7794
+     */
+    public function testSetScheme()
+    {
+        $page = new Zend_Navigation_Page_Mvc();
+        $page->setScheme('https');
+
+        $this->assertEquals('https', $page->getScheme());
+    }
+
+    /**
+     * @group ZF-7794
+     */
+    public function testOptionScheme()
+    {
+        $page = new Zend_Navigation_Page_Mvc(
+            array(
+                 'scheme' => 'https',
+            )
+        );
+
+        $this->assertEquals('https', $page->getScheme());
+    }
+
+    /**
+     * @group ZF-7794
+     */
+    public function testHrefGeneratedWithScheme()
+    {
+        $page = new Zend_Navigation_Page_Mvc(array(
+            'controller' => 'foo',
+            'action'     => 'bar',
+            'scheme'     => 'https',
+        ));
+
+        $_SERVER['HTTP_HOST'] = 'foobar.example.com';
+
+        $this->assertEquals(
+            'https://foobar.example.com/foo/bar',
+            $page->getHref()
+        );
+    }
+
+    /**
+     * @group ZF-7794
+     */
+    public function testHrefGeneratedWithSchemeIsRouteAware()
+    {
+        $page = new Zend_Navigation_Page_Mvc(array(
+            'action'     => 'myaction',
+            'controller' => 'mycontroller',
+            'route'      => 'myroute',
+            'params'     => array(
+                'page' => 1337,
+            ),
+            'scheme'     => 'https',
+        ));
+
+        $this->_front->getRouter()->addRoute(
+            'myroute',
+            new Zend_Controller_Router_Route(
+                'lolcat/:action/:page',
+                array(
+                    'module'     => 'default',
+                    'controller' => 'foobar',
+                    'action'     => 'bazbat',
+                    'page'       => 1,
+                )
+            )
+        );
+
+        $_SERVER['HTTP_HOST'] = 'foobar.example.com';
+
+        $this->assertEquals(
+            'https://foobar.example.com/lolcat/myaction/1337',
+            $page->getHref()
+        );
+    }
+
     public function testToArrayMethod()
     {
         $options = array(
-            'label' => 'foo',
-            'action' => 'index',
+            'label'      => 'foo',
+            'action'     => 'index',
             'controller' => 'index',
-            'module' => 'test',
-            'fragment' => 'bar',
-            'id' => 'my-id',
-            'class' => 'my-class',
-            'title' => 'my-title',
-            'target' => 'my-target',
-            'order' => 100,
-            'active' => true,
-            'visible' => false,
+            'module'     => 'test',
+            'fragment'   => 'bar',
+            'id'         => 'my-id',
+            'class'      => 'my-class',
+            'title'      => 'my-title',
+            'target'     => 'my-target',
+            'order'      => 100,
+            'active'     => true,
+            'visible'    => false,
             'encodeUrl'  => false,
-
-            'foo' => 'bar',
-            'meaning' => 42
+            'scheme'     => 'https',
+            'foo'        => 'bar',
+            'meaning'    => 42
         );
 
         $page = new Zend_Navigation_Page_Mvc($options);
@@ -585,13 +665,15 @@ class Zend_Navigation_Page_MvcTest extends PHPUnit_Framework_TestCase
         $toArray = $page->toArray();
 
         $options['reset_params'] = true;
-        $options['route'] = null;
-        $options['params'] = array();
-        $options['rel'] = array();
-        $options['rev'] = array();
-
-        $this->assertEquals(array(),
-            array_diff_assoc($options, $page->toArray()));
+        $options['route']        = null;
+        $options['params']       = array();
+        $options['rel']          = array();
+        $options['rev']          = array();
+
+        $this->assertEquals(
+            array(),
+            array_diff_assoc($options, $page->toArray())
+        );
     }
 
     public function testSpecifyingAnotherUrlHelperToGenerateHrefs()
@@ -614,6 +696,32 @@ class Zend_Navigation_Page_MvcTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group ZF-7794
+     */
+    public function testSpecifyingAnotherSchemeHelperToGenerateHrefs()
+    {
+        $path = dirname(dirname(__FILE__)) . '/_files/My/SchemeHelper.php';
+        require_once $path;
+
+        $newHelper = new My_SchemeHelper();
+        Zend_Navigation_Page_Mvc::setSchemeHelper($newHelper);
+
+        $page = new Zend_Navigation_Page_Mvc(
+            array(
+                 'scheme' => 'https',
+            )
+        );
+
+        $expected = My_SchemeHelper::RETURN_URL;
+        $actual   = $page->getHref();
+
+        $old = new Zend_View_Helper_ServerUrl();
+        Zend_Navigation_Page_Mvc::setSchemeHelper($old);
+
+        $this->assertEquals($expected, $actual);
+    }
+
+    /**
      * @group ZF-11550
      */
     public function testNullValuesInMatchedRouteWillStillReturnMatchedPage()

+ 40 - 0
tests/Zend/Navigation/_files/My/SchemeHelper.php

@@ -0,0 +1,40 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Navigation
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: UrlHelper.php 24593 2012-01-05 20:35:02Z matthew $
+ */
+
+require_once 'Zend/View/Helper/ServerUrl.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Navigation
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class My_SchemeHelper extends Zend_View_Helper_ServerUrl
+{
+    const RETURN_URL = 'spotify:track:2nd6CTjR9zjHGT0QtpfLHe';
+
+    public function serverUrl($requestUri = null)
+    {
+        return self::RETURN_URL;
+    }
+}