public function testInvalidTagObject() { $json = API::getItemTemplate("book"); $json->tags[] = array("invalid"); $response = API::postItem($json); $this->assert400ForObject($response, "Tag must be an object"); }
public function setUp() { parent::setUp(); // Create too-long note content $this->content = str_repeat("1234567890", 25001); // Create JSON template $this->json = API::getItemTemplate("note"); $this->json->note = $this->content; }
public function testZoteroWriteToken() { $json = API::getItemTemplate("book"); $token = md5(uniqid()); $response = API::userPost(self::$config['userID'], "items?key=" . self::$config['apiKey'], json_encode(array("items" => array($json))), array("Content-Type: application/json", "Zotero-Write-Token: {$token}")); $this->assert200ForObject($response); $response = API::userPost(self::$config['userID'], "items?key=" . self::$config['apiKey'], json_encode(array("items" => array($json))), array("Content-Type: application/json", "Zotero-Write-Token: {$token}")); $this->assert412($response); }
public function testCreateItemWithChildren() { $json = API::getItemTemplate("newspaperArticle"); $noteJSON = API::getItemTemplate("note"); $noteJSON->note = "<p>Here's a test note</p>"; $json->notes = array($noteJSON); $response = API::userPost(self::$config['userID'], "items?key=" . self::$config['apiKey'], json_encode(array("items" => array($json)))); $this->assert201($response); $xml = API::getXMLFromResponse($response); $this->assertNumResults(1, $response); $this->assertEquals(1, (int) array_shift($xml->xpath('//atom:entry/zapi:numChildren'))); }
public function testRelatedItemRelationsSingleRequest() { $uriPrefix = "http://zotero.org/users/" . self::$config['userID'] . "/items/"; // TEMP: Use autoloader require_once '../../model/Utilities.inc.php'; require_once '../../model/ID.inc.php'; $item1Key = \Zotero_ID::getKey(); $item2Key = \Zotero_ID::getKey(); $item1URI = $uriPrefix . $item1Key; $item2URI = $uriPrefix . $item2Key; $item1JSON = API::getItemTemplate('book'); $item1JSON->itemKey = $item1Key; $item1JSON->itemVersion = 0; $item1JSON->relations->{'dc:relation'} = $item2URI; $item2JSON = API::getItemTemplate('book'); $item2JSON->itemKey = $item2Key; $item2JSON->itemVersion = 0; $response = API::postItems([$item1JSON, $item2JSON]); $this->assert200($response); $json = API::getJSONFromResponse($response); // Make sure it exists on item 1 $xml = API::getItemXML($item1JSON->itemKey); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content'], true); $this->assertCount(1, $json['relations']); $this->assertEquals($item2URI, $json['relations']['dc:relation']); // And item 2, since related items are bidirectional $xml = API::getItemXML($item2JSON->itemKey); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content'], true); $this->assertCount(1, $json['relations']); $this->assertEquals($item1URI, $json['relations']['dc:relation']); }
public function testNewInvalidBookItem() { $json = API::getItemTemplate("book"); // Missing item type $json2 = clone $json; unset($json2->itemType); $response = API::userPost(self::$config['userID'], "items?key=" . self::$config['apiKey'], json_encode(array("items" => array($json2))), array("Content-Type: application/json")); $this->assert400ForObject($response, "'itemType' property not provided"); // contentType on non-attachment $json2 = clone $json; $json2->contentType = "text/html"; $response = API::userPost(self::$config['userID'], "items?key=" . self::$config['apiKey'], json_encode(array("items" => array($json2))), array("Content-Type: application/json")); $this->assert400ForObject($response, "'contentType' is valid only for attachment items"); // more tests }
private function _testMultiObjectLastModifiedVersion($objectType) { $objectTypePlural = API::getPluralObjectType($objectType); $objectKeyProp = $objectType . "Key"; $objectVersionProp = $objectType . "Version"; $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'] . "&limit=1"); $version = $response->getHeader("Last-Modified-Version"); $this->assertTrue(is_numeric($version)); switch ($objectType) { case 'collection': $json = new \stdClass(); $json->name = "Name"; break; case 'item': $json = API::getItemTemplate("book"); break; case 'search': $json = new \stdClass(); $json->name = "Name"; $json->conditions = array(array("condition" => "title", "operator" => "contains", "value" => "test")); break; } // Outdated library version $response = API::userPost(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'], json_encode(array($objectTypePlural => array($json))), array("Content-Type: application/json", "If-Unmodified-Since-Version: " . ($version - 1))); $this->assert412($response); // Make sure version didn't change during failure $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'] . "&limit=1"); $this->assertEquals($version, $response->getHeader("Last-Modified-Version")); // Create a new object, using library timestamp $response = API::userPost(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'], json_encode(array($objectTypePlural => array($json))), array("Content-Type: application/json", "If-Unmodified-Since-Version: {$version}")); $this->assert200($response); $version2 = $response->getHeader("Last-Modified-Version"); $this->assertTrue(is_numeric($version2)); // Version should be incremented on new object $this->assertGreaterThan($version, $version2); $objectKey = API::getFirstSuccessKeyFromResponse($response); // Check single-object request $response = API::userGet(self::$config['userID'], "{$objectTypePlural}/{$objectKey}?key=" . self::$config['apiKey'] . "&content=json"); $this->assert200($response); $version = $response->getHeader("Last-Modified-Version"); $this->assertTrue(is_numeric($version)); $this->assertEquals($version, $version2); $json = json_decode(API::getContentFromResponse($response)); // Modify object $json->{$objectKeyProp} = $objectKey; switch ($objectType) { case 'collection': $json->name = "New Name"; break; case 'item': $json->title = "New Title"; break; case 'search': $json->name = "New Name"; break; } // No If-Unmodified-Since-Version or object version property unset($json->{$objectVersionProp}); $response = API::userPost(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'], json_encode(array($objectTypePlural => array($json))), array("Content-Type: application/json")); $this->assert428ForObject($response); // Outdated object version property $json->{$objectVersionProp} = $version - 1; $response = API::userPost(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'], json_encode(array($objectTypePlural => array($json))), array("Content-Type: application/json")); $this->assert412ForObject($response, ucwords($objectType) . " has been modified since specified version " . "(expected {$json->{$objectVersionProp}}, found {$version})"); // Modify object, using object version property $json->{$objectVersionProp} = $version; $response = API::userPost(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'], json_encode(array($objectTypePlural => array($json))), array("Content-Type: application/json")); $this->assert200($response); // Version should be incremented on modified object $version3 = $response->getHeader("Last-Modified-Version"); $this->assertTrue(is_numeric($version3)); $this->assertGreaterThan($version2, $version3); // Check library version $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey']); $version = $response->getHeader("Last-Modified-Version"); $this->assertTrue(is_numeric($version)); $this->assertEquals($version, $version3); // Check single-object request $response = API::userGet(self::$config['userID'], "{$objectTypePlural}/{$objectKey}?key=" . self::$config['apiKey']); $version = $response->getHeader("Last-Modified-Version"); $this->assertTrue(is_numeric($version)); $this->assertEquals($version, $version3); // TODO: Version should be incremented on deleted item }
private function _testPartialWriteFailureWithUnchanged($objectType) { API::userClear(self::$config['userID']); $objectTypePlural = API::getPluralObjectType($objectType); switch ($objectType) { case 'collection': $data = API::createCollection("Test", false, $this, 'data'); $json1 = json_decode($data['content']); $json2 = array("name" => str_repeat("1234567890", 6554)); $json3 = array("name" => "Test"); break; case 'item': $data = API::createItem("book", array("title" => "Title"), $this, 'data'); $json1 = json_decode($data['content']); $json2 = API::getItemTemplate('book'); $json3 = clone $json2; $json2->title = str_repeat("1234567890", 6554); break; case 'search': $conditions = array(array('condition' => 'title', 'operator' => 'contains', 'value' => 'value')); $data = API::createSearch("Name", $conditions, $this, 'data'); $json1 = json_decode($data['content']); $json2 = array("name" => str_repeat("1234567890", 6554), "conditions" => $conditions); $json3 = array("name" => "Test", "conditions" => $conditions); break; } $response = API::userPost(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'], json_encode(array("{$objectTypePlural}" => array($json1, $json2, $json3))), array("Content-Type: application/json")); $this->assert200($response); $json = API::getJSONFromResponse($response); $this->assertUnchangedForObject($response, false, 0); $this->assert400ForObject($response, false, 1); $this->assert200ForObject($response, false, 2); $json = API::getJSONFromResponse($response); $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?format=keys&key=" . self::$config['apiKey']); $this->assert200($response); $keys = explode("\n", trim($response->getBody())); $this->assertCount(2, $keys); foreach ($json['success'] as $key) { $this->assertContains($key, $keys); } }