Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 24 deletions.
48 changes: 47 additions & 1 deletion src/AbstractPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ abstract class AbstractPage extends Container
*/
protected $_label;

/**
* Fragment identifier (anchor identifier)
*
* The fragment identifier (anchor identifier) pointing to an anchor within
* a resource that is subordinate to another, primary resource.
* The fragment identifier introduced by a hash mark "#".
* Example: http://www.example.org/foo.html#bar ("bar" is the fragment identifier)
*
* @link http://www.w3.org/TR/html401/intro/intro.html#fragment-uri
*
* @var string|null
*/
protected $_fragment;

/**
* Page id
*
Expand Down Expand Up @@ -319,6 +333,34 @@ public function getLabel()
return $this->_label;
}

/**
* Sets a fragment identifier
*
* @param string $fragment new fragment identifier
* @return Zend_Navigation_Page fluent interface, returns self
* @throws Zend_Navigation_Exception if empty/no string is given
*/
public function setFragment($fragment)
{
if (null !== $fragment && !is_string($fragment)) {
throw new Exception\InvalidArgumentException(
'Invalid argument: $fragment must be a string or null');
}

$this->_fragment = $fragment;
return $this;
}

/**
* Returns fragment identifier
*
* @return string|null fragment identifier
*/
public function getFragment()
{
return $this->_fragment;
}

/**
* Sets page id
*
Expand Down Expand Up @@ -722,6 +764,9 @@ public function getActive($recursive = false)
*/
public function setVisible($visible = true)
{
if (is_string($visible) && 'false' == strtolower($visible)) {
$visible = false;
}
$this->_visible = (bool) $visible;
return $this;
}
Expand Down Expand Up @@ -1068,7 +1113,8 @@ public function toArray()
return array_merge(
$this->getCustomProperties(),
array(
'label' => $this->getlabel(),
'label' => $this->getLabel(),
'fragment' => $this->getFragment(),
'id' => $this->getId(),
'class' => $this->getClass(),
'title' => $this->getTitle(),
Expand Down
49 changes: 47 additions & 2 deletions src/Page/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ class Mvc extends AbstractPage
*/
protected $_resetParams = true;

/**
* Whether href should be encoded when assembling URL
*
* @see getHref()
* @var bool
*/
protected $_encodeUrl = true;

/**
* Cached href
*
Expand Down Expand Up @@ -193,7 +201,14 @@ public function getHref()

$url = self::$_urlHelper->__invoke($params,
$this->getRoute(),
$this->getResetParams());
$this->getResetParams(),
$this->getEncodeUrl());

// Add the fragment identifier if it is set
$fragment = $this->getFragment();
if (null !== $fragment) {
$url .= '#' . $fragment;
}

return $this->_hrefCache = $url;
}
Expand Down Expand Up @@ -392,6 +407,35 @@ public function getResetParams()
return $this->_resetParams;
}

/**
* Sets whether href should be encoded when assembling URL
*
* @see getHref()
*
* @param bool $resetParams whether href should be encoded when
* assembling URL
* @return \Zend\Navigation\Page\Mvc fluent interface, returns self
*/
public function setEncodeUrl($encodeUrl)
{
$this->_encodeUrl = (bool) $encodeUrl;
$this->_hrefCache = null;

return $this;
}

/**
* Returns whether herf should be encoded when assembling URL
*
* @see getHref()
*
* @return bool whether herf should be encoded when assembling URL
*/
public function getEncodeUrl()
{
return $this->_encodeUrl;
}

/**
* Sets action helper for assembling URLs
*
Expand Down Expand Up @@ -422,7 +466,8 @@ public function toArray()
'module' => $this->getModule(),
'params' => $this->getParams(),
'route' => $this->getRoute(),
'reset_params' => $this->getResetParams()
'reset_params' => $this->getResetParams(),
'encodeUrl' => $this->getEncodeUrl(),
));
}
}
15 changes: 14 additions & 1 deletion src/Page/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,25 @@ public function getUri()

/**
* Returns href for this page
*
* Includes the fragment identifier if it is set.
*
* @return string
*/
public function getHref()
{
return $this->getUri();
$uri = $this->getUri();

$fragment = $this->getFragment();
if (null !== $fragment) {
if ('#' == substr($uri, -1)) {
return $uri . $fragment;
} else {
return $uri . '#' . $fragment;
}
}

return $uri;
}

// Public methods:
Expand Down
12 changes: 6 additions & 6 deletions test/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ public function testFinderMethodsShouldWorkWithCustomProperties()
$nav = $this->_getFindByNavigation();

$found = $nav->findOneBy('page2', 'page2');
$this->assertType('Zend\Navigation\AbstractPage', $found);
$this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found);
$this->assertEquals('Page 2', $found->getLabel());
}

Expand All @@ -773,7 +773,7 @@ public function testFindOneByShouldReturnOnlyOnePage()
$nav = $this->_getFindByNavigation();

$found = $nav->findOneBy('id', 'page_2_and_3');
$this->assertType('Zend\Navigation\AbstractPage', $found);
$this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found);
$this->assertEquals('Page 2', $found->getLabel());
}

Expand Down Expand Up @@ -817,15 +817,15 @@ public function testFindByShouldDefaultToFindOneBy()
$nav = $this->_getFindByNavigation();

$found = $nav->findBy('id', 'page_2_and_3');
$this->assertType('Zend\Navigation\AbstractPage', $found);
$this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found);
}

public function testFindOneByMagicMethodNativeProperty()
{
$nav = $this->_getFindByNavigation();

$found = $nav->findOneById('page_2_and_3');
$this->assertType('Zend\Navigation\AbstractPage', $found);
$this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found);
$this->assertEquals('Page 2', $found->getLabel());
}

Expand All @@ -834,7 +834,7 @@ public function testFindOneByMagicMethodCustomProperty()
$nav = $this->_getFindByNavigation();

$found = $nav->findOneBypage2('page2');
$this->assertType('Zend\Navigation\AbstractPage', $found);
$this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found);
$this->assertEquals('Page 2', $found->getLabel());
}

Expand Down Expand Up @@ -891,7 +891,7 @@ public function testFindByMagicMethodIsEquivalentToFindOneBy()
$nav = $this->_getFindByNavigation();

$found = $nav->findById('page_2_and_3');
$this->assertType('Zend\Navigation\AbstractPage', $found);
$this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found);
$this->assertEquals('Page 2', $found->getLabel());
}

Expand Down
94 changes: 88 additions & 6 deletions test/Page/MvcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,38 @@ public function testHrefGeneratedIsRouteAware()
$this->assertEquals('/lolcat/myaction/1337', $page->getHref());
}

/**
* @group ZF-8922
*/
public function testGetHrefWithFragmentIdentifier()
{
$page = new Page\Mvc(array(
'label' => 'foo',
'fragment' => 'qux',
'controller' => 'mycontroller',
'action' => 'myaction',
'route' => 'myroute',
'params' => array(
'page' => 1337
)
));

$this->_front->getRouter()->addRoute(
'myroute',
new \Zend\Controller\Router\Route\Route(
'lolcat/:action/:page',
array(
'module' => 'default',
'controller' => 'foobar',
'action' => 'bazbat',
'page' => 1
)
)
);

$this->assertEquals('/lolcat/myaction/1337#qux', $page->getHref());
}

public function testIsActiveReturnsTrueOnIdenticalModuleControllerAction()
{
$page = new Page\Mvc(array(
Expand All @@ -117,12 +149,12 @@ public function testIsActiveReturnsTrueOnIdenticalModuleControllerAction()
));

$this->_front->getRequest()->setParams(array(
'module' => 'default',
'module' => 'application',
'controller' => 'index',
'action' => 'index'
));

$this->assertEquals(true, $page->isActive());
$this->assertTrue($page->isActive());
}

public function testIsActiveReturnsFalseOnDifferentModuleControllerAction()
Expand All @@ -139,7 +171,7 @@ public function testIsActiveReturnsFalseOnDifferentModuleControllerAction()
'action' => 'index'
));

$this->assertEquals(false, $page->isActive());
$this->assertFalse($page->isActive());
}

public function testIsActiveReturnsTrueOnIdenticalIncludingPageParams()
Expand All @@ -161,7 +193,7 @@ public function testIsActiveReturnsTrueOnIdenticalIncludingPageParams()
'id' => '1337'
));

$this->assertEquals(true, $page->isActive());
$this->assertTrue($page->isActive());
}

public function testIsActiveReturnsTrueWhenRequestHasMoreParams()
Expand All @@ -180,7 +212,7 @@ public function testIsActiveReturnsTrueWhenRequestHasMoreParams()
'id' => '1337'
));

$this->assertEquals(true, $page->isActive());
$this->assertTrue($page->isActive());
}

public function testIsActiveReturnsFalseWhenRequestHasLessParams()
Expand All @@ -202,7 +234,7 @@ public function testIsActiveReturnsFalseWhenRequestHasLessParams()
'id' => null
));

$this->assertEquals(false, $page->isActive());
$this->assertFalse($page->isActive());
}

public function testActionAndControllerAccessors()
Expand Down Expand Up @@ -317,20 +349,70 @@ public function testSetAndGetParams()
$this->assertEquals(array(), $page->getParams());
}

/**
* @group ZF-10465
*/
public function testSetAndGetEncodeUrl()
{
$page = new Page\Mvc(array(
'label' => 'foo',
'action' => 'index',
'controller' => 'index',
));

$page->setEncodeUrl(false);
$this->assertEquals(false, $page->getEncodeUrl());
}

/**
* @group ZF-10465
*/
public function testEncodeUrlIsRouteAware()
{
$page = new Page\Mvc(array(
'label' => 'foo',
'route' => 'myroute',
'encodeUrl' => false,
'params' => array(
'contentKey' => 'pagexy/subpage',
)
));

$this->_front->getRouter()->addRoute(
'myroute',
new \Zend\Controller\Router\Route\Regex(
'(.+)\.html',
array(
'module' => 'default',
'controller' => 'foobar',
'action' => 'bazbat',
),
array(
1 => 'contentKey'
),
'%s.html'
)
);

$this->assertEquals('/pagexy/subpage.html', $page->getHref());
}

public function testToArrayMethod()
{
$options = array(
'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,
'encodeUrl' => false,

'foo' => 'bar',
'meaning' => 42
Expand Down
Loading

0 comments on commit 4449c16

Please sign in to comment.