public function testDeleteCollectionRelation() { $relations = array("owl:sameAs" => "http://zotero.org/groups/1/collections/AAAAAAAA"); $data = API::createCollection("Test", array("relations" => $relations), $this, 'data'); $json = json_decode($data['content'], true); // Remove all relations $json['relations'] = new \stdClass(); unset($relations['owl:sameAs']); $response = API::userPut(self::$config['userID'], "collections/{$data['key']}?key=" . self::$config['apiKey'], json_encode($json)); $this->assert204($response); // Make sure it's gone $xml = API::getCollectionXML($data['key']); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content'], true); $this->assertCount(sizeOf($relations), $json['relations']); foreach ($relations as $predicate => $object) { $this->assertEquals($object, $json['relations'][$predicate]); } }
public function testCollectionItemChange() { $collectionKey1 = API::createCollection('Test', false, $this, 'key'); $collectionKey2 = API::createCollection('Test', false, $this, 'key'); $xml = API::createItem("book", array('collections' => array($collectionKey1)), $this, 'atom'); $data = API::parseDataFromAtomEntry($xml); $itemKey1 = $data['key']; $itemVersion1 = $data['version']; $json = json_decode($data['content']); $this->assertEquals(array($collectionKey1), $json->collections); $xml = API::createItem("journalArticle", array('collections' => array($collectionKey2)), $this, 'atom'); $data = API::parseDataFromAtomEntry($xml); $itemKey2 = $data['key']; $itemVersion2 = $data['version']; $json = json_decode($data['content']); $this->assertEquals(array($collectionKey2), $json->collections); $xml = API::getCollectionXML($collectionKey1); $collectionData1 = API::parseDataFromAtomEntry($xml); $this->assertEquals(1, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems'))); $xml = API::getCollectionXML($collectionKey2); $collectionData2 = API::parseDataFromAtomEntry($xml); $this->assertEquals(1, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems'))); $libraryVersion = API::getLibraryVersion(); // Add items to collection $response = API::userPatch(self::$config['userID'], "items/{$itemKey1}?key=" . self::$config['apiKey'], json_encode(array("collections" => array($collectionKey1, $collectionKey2))), array("Content-Type: application/json", "If-Unmodified-Since-Version: {$itemVersion1}")); $this->assert204($response); // Item version should change $xml = API::getItemXML($itemKey1); $data = API::parseDataFromAtomEntry($xml); $this->assertEquals($libraryVersion + 1, $data['version']); // Collection timestamp shouldn't change, but numItems should $xml = API::getCollectionXML($collectionKey2); $data = API::parseDataFromAtomEntry($xml); $this->assertEquals(2, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems'))); $this->assertEquals($collectionData2['version'], $data['version']); $collectionData2 = $data; $libraryVersion = API::getLibraryVersion(); // Remove collections $response = API::userPatch(self::$config['userID'], "items/{$itemKey2}?key=" . self::$config['apiKey'], json_encode(array("collections" => array())), array("Content-Type: application/json", "If-Unmodified-Since-Version: {$itemVersion2}")); $this->assert204($response); // Item version should change $xml = API::getItemXML($itemKey2); $data = API::parseDataFromAtomEntry($xml); $this->assertEquals($libraryVersion + 1, $data['version']); // Collection timestamp shouldn't change, but numItems should $xml = API::getCollectionXML($collectionKey2); $data = API::parseDataFromAtomEntry($xml); $this->assertEquals(1, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems'))); $this->assertEquals($collectionData2['version'], $data['version']); // Check collections arrays and numItems $xml = API::getItemXML($itemKey1); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content']); $this->assertCount(2, $json->collections); $this->assertContains($collectionKey1, $json->collections); $this->assertContains($collectionKey2, $json->collections); $xml = API::getItemXML($itemKey2); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content']); $this->assertCount(0, $json->collections); $xml = API::getCollectionXML($collectionKey1); $this->assertEquals(1, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems'))); $xml = API::getCollectionXML($collectionKey2); $this->assertEquals(1, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems'))); }
private function _testUploadUnmodified($objectType) { $objectTypePlural = API::getPluralObjectType($objectType); switch ($objectType) { case 'collection': $xml = API::createCollection("Name", false, $this); break; case 'item': $xml = API::createItem("book", array("title" => "Title"), $this); break; case 'search': $xml = API::createSearch("Name", 'default', $this); break; } $version = (int) array_shift($xml->xpath('//atom:entry/zapi:version')); $this->assertNotEquals(0, $version); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content']); $response = API::userPut(self::$config['userID'], "{$objectTypePlural}/{$data['key']}?key=" . self::$config['apiKey'], json_encode($json)); $this->assert204($response); $this->assertEquals($version, $response->getHeader("Last-Modified-Version")); switch ($objectType) { case 'collection': $xml = API::getCollectionXML($data['key']); break; case 'item': $xml = API::getItemXML($data['key']); break; case 'search': $xml = API::getSearchXML($data['key']); break; } $data = API::parseDataFromAtomEntry($xml); $this->assertEquals($version, $data['version']); }
public function testEditSingleCollection() { API::useAPIVersion(2); $xml = API::createCollection("Test", false, $this); $data = API::parseDataFromAtomEntry($xml); $key = $data['key']; $version = $data['version']; API::useAPIVersion(1); $xml = API::getCollectionXML($data['key']); $etag = (string) array_shift($xml->xpath('//atom:entry/atom:content/@etag')); $this->assertNotNull($etag); $newName = "Test 2"; $json = array("name" => $newName, "parent" => false); $response = API::userPut(self::$config['userID'], "collections/{$key}?key=" . self::$config['apiKey'], json_encode($json), array("Content-Type: application/json", "If-Match: {$etag}")); $this->assert200($response); $xml = API::getXMLFromResponse($response); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content']); $this->assertEquals($newName, (string) $json->name); }