From b487372a3cc2258e78d1acd46aec10b9a33f7fde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Ebn=C3=B6ther?= Date: Wed, 18 Aug 2010 17:07:45 +0200 Subject: [PATCH] * Implemented jackalope_transport_DavexClient::checkLogin to make sure the session is active * Refactored tests for getNodeTypes to not use actual backend. --- src/jackalope/transport/DavexClient.php | 38 ++++++++------ tests/fixtures/empty.xml | 2 + tests/fixtures/nodetypes.xml | 9 ++-- tests/fixtures/small_nodetypes.xml | 23 +++++++++ tests/transport/DavexClient.php | 67 +++++++++++++++++++++++-- 5 files changed, 115 insertions(+), 24 deletions(-) create mode 100644 tests/fixtures/empty.xml create mode 100644 tests/fixtures/small_nodetypes.xml diff --git a/src/jackalope/transport/DavexClient.php b/src/jackalope/transport/DavexClient.php index d14f391c..2d69e625 100644 --- a/src/jackalope/transport/DavexClient.php +++ b/src/jackalope/transport/DavexClient.php @@ -120,16 +120,14 @@ public function getRepositoryDescriptors() { * TODO: should we call this getNode? does not work for property. (see ObjectManager::getPropertyByPath for more on properties) * @param path absolute path to item * @return array for the node (decoded from json) + * @throws PHPCR_RepositoryException if now logged in */ public function getItem($path) { if ('/' != substr($path, 0, 1)) { //sanity check throw new PHPCR_RepositoryException("Implementation error: '$path' is not an absolute path"); } - if (empty($this->workspaceUri)) { - throw new PHPCR_RepositoryException("Implementation error: Please login before accessing content"); - } - + $this->checkLogin(); return $this->getJsonFromBackend(self::GET, $this->workspaceUriRoot . $path . '.0.json'); } /** @@ -137,12 +135,11 @@ public function getItem($path) { * @param uuid the id in JCR format * @return string path to the node * @throws PHPCR_ItemNotFoundException if the backend does not know the uuid + * @throws PHPCR_RepositoryException if now logged in */ public function getNodePathForIdentifier($uuid) { - if (empty($this->workspaceUri)) { - throw new PHPCR_RepositoryException("Implementation error: Please login before accessing content"); - } - + $this->checkLogin(); + $dom = $this->getDomFromBackend(self::REPORT, $this->workspaceUri, self::buildLocateRequest($uuid)); /* answer looks like @@ -167,11 +164,10 @@ public function getNodePathForIdentifier($uuid) { /** * get the registered namespaces mappings from the backend * @return associative array of prefix => uri + * @throws PHPCR_RepositoryException if now logged in */ public function getNamespaces() { - if (empty($this->workspaceUri)) { - throw new PHPCR_RepositoryException("Implementation error: Please login before accessing content"); - } + $this->checkLogin(); $dom = $this->getDomFromBackend(self::REPORT, $this->workspaceUri, self::buildReportRequest('dcr:registerednamespaces')); @@ -191,13 +187,13 @@ public function getNamespaces() { * Returns node types * @param array nodetypes to request * @return dom with the definitions + * @throws PHPCR_RepositoryException if now logged in */ public function getNodeTypes($nodeTypes = array()) { - if (empty($this->workspaceUri)) { - throw new PHPCR_RepositoryException("Implementation error: Please login before accessing content"); - } + $this->checkLogin(); + $dom = $this->getDomFromBackend( - self::REPORT, $this->workspaceUri . '/jcr:root/tests_level1_access_base', + self::REPORT, $this->workspaceUri . '/jcr:root', self::buildNodeTypesRequest($nodeTypes) ); if ($dom->firstChild->localName != 'nodeTypes') { @@ -205,7 +201,17 @@ public function getNodeTypes($nodeTypes = array()) { } return $dom; } - + + /** + * Throws an error if the there is no login + * @throws PHPCR_RepositoryException if now logged in + */ + protected function checkLogin() { + if (empty($this->workspaceUri)) { + throw new PHPCR_RepositoryException("Implementation error: Please login before accessing content"); + } + } + /** * Returns the XML required to request nodetypes * @param array the nodetypes you want to request diff --git a/tests/fixtures/empty.xml b/tests/fixtures/empty.xml new file mode 100644 index 00000000..08e6c406 --- /dev/null +++ b/tests/fixtures/empty.xml @@ -0,0 +1,2 @@ + + diff --git a/tests/fixtures/nodetypes.xml b/tests/fixtures/nodetypes.xml index 6d74bb65..d7d01580 100644 --- a/tests/fixtures/nodetypes.xml +++ b/tests/fixtures/nodetypes.xml @@ -1,7 +1,5 @@ - - - - + + @@ -1575,3 +1573,6 @@ + + + diff --git a/tests/fixtures/small_nodetypes.xml b/tests/fixtures/small_nodetypes.xml new file mode 100644 index 00000000..4835bebb --- /dev/null +++ b/tests/fixtures/small_nodetypes.xml @@ -0,0 +1,23 @@ + + + + + nt:hierarchyNode + + + + nt:hierarchyNode + + + + + + nt:hierarchyNode + + + + nt:base + + + + diff --git a/tests/transport/DavexClient.php b/tests/transport/DavexClient.php index 971c8f3a..c480eaec 100644 --- a/tests/transport/DavexClient.php +++ b/tests/transport/DavexClient.php @@ -1,6 +1,12 @@ config['url']); @@ -17,6 +23,15 @@ public function testGetRepositoryDescriptors() { } } } + + /** + * @expectedException PHPCR_RepositoryException + */ + public function testCheckLoginFail() { + $t = new jackalope_transport_DavexClient('http://localhost:1/server'); + $t->getNodeTypes(); + } + /** * @expectedException PHPCR_RepositoryException */ @@ -71,13 +86,57 @@ public function testGetNamespaces() { } } + /** START TESTING NODE TYPES **/ + protected function setUpNodeTypeMock($params, $fixture) { + $dom = new DOMDocument(); + $dom->load($fixture); + + $requestStr = jackalope_transport_DavexClient_Mock::buildNodeTypesRequestMock($params); + + $t = $this->getMock('jackalope_transport_DavexClient', array('getDomFromBackend', 'checkLogin', '__construct'), array(null)); + $t->expects($this->once()) + ->method('getDomFromBackend') + ->with(jackalope_transport_DavexClient::REPORT, '/jcr:root', $requestStr) + ->will($this->returnValue($dom)); + return $t; + } + + public function testGetAllNodeTypesRequest() { + $xmlStr = ''; + $this->assertEquals($xmlStr, jackalope_transport_DavexClient_Mock::buildNodeTypesRequestMock(array())); + } + + public function testSpecificNodeTypesRequest() { + $xmlStr= 'foobarfoobar'; + $this->assertEquals($xmlStr, jackalope_transport_DavexClient_Mock::buildNodeTypesRequestMock(array('foo', 'bar', 'foobar'))); + } + public function testGetNodeTypes() { - $t = new jackalope_transport_DavexClient($this->config['url']); - $x = $t->login($this->credentials, $this->config['workspace']); - $this->assertTrue($x); + $t = $this->setUpNodeTypeMock(array(), 'fixtures/nodetypes.xml'); + $nt = $t->getNodeTypes(); $this->assertTrue($nt instanceOf DOMDocument); + $this->assertEquals('mix:created', $nt->firstChild->firstChild->getAttribute('name')); + } + + public function testSpecificGetNodeTypes() { + $t = $this->setUpNodeTypeMock(array('nt:folder', 'nt:file'), 'fixtures/small_nodetypes.xml'); + $nt = $t->getNodeTypes(array('nt:folder', 'nt:file')); - $this->assertTrue($nt instanceOf DOMDocument); + $this->assertType('DOMDocument', $nt); + $xp = new DOMXpath($nt); + $res = $xp->query('//nodeType'); + $this->assertEquals(2, $res->length); + $this->assertEquals('nt:folder', $res->item(0)->getAttribute('name')); + $this->assertEquals('nt:file', $res->item(1)->getAttribute('name')); + } + + public function testEmptyGetNodeTypes() { + $t = $this->setUpNodeTypeMock(array(), 'fixtures/empty.xml'); + + $this->setExpectedException('PHPCR_RepositoryException'); + $nt = $t->getNodeTypes(); } + + /** END TESTING NODE TYPES **/ }