public function testCreatorSummaryAtom() { $xml = API::createItem("book", array("creators" => array(array("creatorType" => "author", "name" => "Test"))), $this, 'atom'); $data = API::parseDataFromAtomEntry($xml); $itemKey = $data['key']; $json = json_decode($data['content'], true); $creatorSummary = (string) array_shift($xml->xpath('//atom:entry/zapi:creatorSummary')); $this->assertEquals("Test", $creatorSummary); $json['creators'][] = array("creatorType" => "author", "firstName" => "Alice", "lastName" => "Foo"); $response = API::userPut(self::$config['userID'], "items/{$itemKey}", json_encode($json)); $this->assert204($response); $xml = API::getItemXML($itemKey); $creatorSummary = (string) array_shift($xml->xpath('//atom:entry/zapi:creatorSummary')); $this->assertEquals("Test and Foo", $creatorSummary); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content'], true); $json['creators'][] = array("creatorType" => "author", "firstName" => "Bob", "lastName" => "Bar"); $response = API::userPut(self::$config['userID'], "items/{$itemKey}", json_encode($json)); $this->assert204($response); $xml = API::getItemXML($itemKey); $creatorSummary = (string) array_shift($xml->xpath('//atom:entry/zapi:creatorSummary')); $this->assertEquals("Test et al.", $creatorSummary); }
private function _testSingleObjectLastModifiedVersion($objectType) { $objectTypePlural = API::getPluralObjectType($objectType); $keyProp = $objectType . "Key"; $versionProp = $objectType . "Version"; switch ($objectType) { case 'collection': $objectKey = API::createCollection("Name", false, $this, 'key'); break; case 'item': $objectKey = API::createItem("book", array("title" => "Title"), $this, 'key'); break; case 'search': $objectKey = API::createSearch("Name", array(array("condition" => "title", "operator" => "contains", "value" => "test")), $this, 'key'); break; } // JSON: Make sure all three instances of the object version // (Last-Modified-Version, 'version', and data.version) // match the library version $response = API::userGet(self::$config['userID'], "{$objectTypePlural}/{$objectKey}"); $this->assert200($response); $objectVersion = $response->getHeader("Last-Modified-Version"); $json = API::getJSONFromResponse($response); $this->assertEquals($objectVersion, $json['version']); $this->assertEquals($objectVersion, $json['data']['version']); // Atom: Make sure all three instances of the object version // (Last-Modified-Version, zapi:version, and the JSON // {$objectType}Version property match the library version $response = API::userGet(self::$config['userID'], "{$objectTypePlural}/{$objectKey}?content=json"); $this->assert200($response); $objectVersion = $response->getHeader("Last-Modified-Version"); $xml = API::getXMLFromResponse($response); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content'], true); $this->assertEquals($objectVersion, $json['version']); $this->assertEquals($objectVersion, $data['version']); $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?limit=1"); $this->assert200($response); $libraryVersion = $response->getHeader("Last-Modified-Version"); $this->assertEquals($libraryVersion, $objectVersion); $this->_modifyJSONObject($objectType, $json); // No If-Unmodified-Since-Version or JSON version property unset($json['version']); $response = API::userPut(self::$config['userID'], "{$objectTypePlural}/{$objectKey}", json_encode($json)); $this->assert428($response); // Out of date version $response = API::userPut(self::$config['userID'], "{$objectTypePlural}/{$objectKey}", json_encode($json), array("If-Unmodified-Since-Version: " . ($objectVersion - 1))); $this->assert412($response); // Update with version header $response = API::userPut(self::$config['userID'], "{$objectTypePlural}/{$objectKey}", json_encode($json), array("If-Unmodified-Since-Version: " . $objectVersion)); $this->assert204($response); $newObjectVersion = $response->getHeader("Last-Modified-Version"); $this->assertGreaterThan($objectVersion, $newObjectVersion); // Update object with JSON version property $this->_modifyJSONObject($objectType, $json); $json['version'] = $newObjectVersion; $response = API::userPut(self::$config['userID'], "{$objectTypePlural}/{$objectKey}", json_encode($json)); $this->assert204($response); $newObjectVersion2 = $response->getHeader("Last-Modified-Version"); $this->assertGreaterThan($newObjectVersion, $newObjectVersion2); // Make sure new library version matches new object version $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?limit=1"); $this->assert200($response); $newLibraryVersion = $response->getHeader("Last-Modified-Version"); $this->assertEquals($newObjectVersion2, $newLibraryVersion); return; // Create an item to increase the library version, and make sure // original object version stays the same API::createItem("book", array("title" => "Title"), $this, 'key'); $response = API::userGet(self::$config['userID'], "{$objectTypePlural}/{$objectKey}?limit=1"); $this->assert200($response); $newObjectVersion2 = $response->getHeader("Last-Modified-Version"); $this->assertEquals($newLibraryVersion, $newObjectVersion2); // // Delete object // // No If-Unmodified-Since-Version $response = API::userDelete(self::$config['userID'], "{$objectTypePlural}/{$objectKey}"); $this->assert428($response); // Outdated If-Unmodified-Since-Version $response = API::userDelete(self::$config['userID'], "{$objectTypePlural}/{$objectKey}", array("If-Unmodified-Since-Version: " . $objectVersion)); $this->assert412($response); // Delete object $response = API::userDelete(self::$config['userID'], "{$objectTypePlural}/{$objectKey}", array("If-Unmodified-Since-Version: " . $newObjectVersion2)); $this->assert204($response); }
public function testNumChildrenAtom() { $xml = API::createItem("book", false, $this, 'atom'); $this->assertEquals(0, (int) array_shift($xml->xpath('/atom:entry/zapi:numChildren'))); $data = API::parseDataFromAtomEntry($xml); $key = $data['key']; API::createAttachmentItem("linked_url", [], $key, $this, 'key'); $response = API::userGet( self::$config['userID'], "items/$key?content=json" ); $xml = API::getXMLFromResponse($response); $this->assertEquals(1, (int) array_shift($xml->xpath('/atom:entry/zapi:numChildren'))); API::createNoteItem("Test", $key, $this, 'key'); $response = API::userGet( self::$config['userID'], "items/$key?content=json" ); $xml = API::getXMLFromResponse($response); $this->assertEquals(2, (int) array_shift($xml->xpath('/atom:entry/zapi:numChildren'))); }
public function testTagNewer() { API::userClear(self::$config['userID']); // Create items with tags API::createItem("book", array("tags" => array(array("tag" => "a"), array("tag" => "b"))), $this); $version = API::getLibraryVersion(); // 'newer' shouldn't return any results $response = API::userGet(self::$config['userID'], "tags?newer={$version}"); $this->assert200($response); $this->assertNumResults(0, $response); // Create another item with tags API::createItem("book", array("tags" => array(array("tag" => "a"), array("tag" => "c"))), $this); // 'newer' should return new tag (Atom) $response = API::userGet(self::$config['userID'], "tags?content=json&newer={$version}"); $this->assert200($response); $this->assertNumResults(1, $response); $this->assertGreaterThan($version, $response->getHeader('Last-Modified-Version')); $xml = API::getXMLFromResponse($response); $data = API::parseDataFromAtomEntry($xml); $data = json_decode($data['content'], true); $this->assertEquals("c", $data['tag']); $this->assertEquals(0, $data['type']); // 'newer' should return new tag (JSON) $response = API::userGet(self::$config['userID'], "tags?newer={$version}"); $this->assert200($response); $this->assertNumResults(1, $response); $this->assertGreaterThan($version, $response->getHeader('Last-Modified-Version')); $json = API::getJSONFromResponse($response)[0]; $this->assertEquals("c", $json['tag']); $this->assertEquals(0, $json['meta']['type']); }
/** * Changing a group's metadata should change its ETag */ public function testUpdateMetadataAtom() { $response = API::userGet(self::$config['userID'], "groups?content=json&key=" . self::$config['apiKey']); $this->assert200($response); // Get group API URI and version $xml = API::getXMLFromResponse($response); $xml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom'); $xml->registerXPathNamespace('zapi', 'http://zotero.org/ns/api'); $groupID = (string) array_shift($xml->xpath("//atom:entry/zapi:groupID")); $url = (string) array_shift($xml->xpath("//atom:entry/atom:link[@rel='self']/@href")); $url = str_replace(self::$config['apiURLPrefix'], '', $url); $version = json_decode(API::parseDataFromAtomEntry($xml)['content'], true)['version']; // Make sure format=versions returns the same ETag $response = API::userGet(self::$config['userID'], "groups?format=versions&key=" . self::$config['apiKey']); $this->assert200($response); $json = json_decode($response->getBody()); $this->assertEquals($version, $json->{$groupID}); // Update group metadata $json = json_decode(array_shift($xml->xpath("//atom:entry/atom:content"))); $xml = new SimpleXMLElement("<group/>"); foreach ($json as $key => $val) { switch ($key) { case 'id': case 'members': continue; case 'name': $name = "My Test Group " . uniqid(); $xml['name'] = $name; break; case 'description': $description = "This is a test description " . uniqid(); $xml->{$key} = $description; break; case 'url': $urlField = "http://example.com/" . uniqid(); $xml->{$key} = $urlField; break; default: $xml[$key] = $val; } } $xml = trim(preg_replace('/^<\\?xml.+\\n/', "", $xml->asXML())); $response = API::put($url, $xml, array("Content-Type: text/xml"), array("username" => self::$config['rootUsername'], "password" => self::$config['rootPassword'])); $this->assert200($response); $xml = API::getXMLFromResponse($response); $xml->registerXPathNamespace('zxfer', 'http://zotero.org/ns/transfer'); $group = $xml->xpath('//atom:entry/atom:content/zxfer:group'); $this->assertCount(1, $group); $this->assertEquals($name, $group[0]['name']); $response = API::userGet(self::$config['userID'], "groups?format=versions&key=" . self::$config['apiKey']); $this->assert200($response); $json = json_decode($response->getBody()); $newVersion = $json->{$groupID}; $this->assertNotEquals($version, $newVersion); // Check ETag header on individual group request $response = API::groupGet($groupID, "?content=json&key=" . self::$config['apiKey']); $this->assert200($response); $this->assertEquals($newVersion, $response->getHeader('Last-Modified-Version')); $json = json_decode(API::getContentFromResponse($response)); $this->assertEquals($name, $json->name); $this->assertEquals($description, $json->description); $this->assertEquals($urlField, $json->url); }