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", json_encode([$json]), array("Content-Type: application/json", "Zotero-Write-Token: {$token}")); $this->assert200ForObject($response); $response = API::userPost(self::$config['userID'], "items", json_encode([$json]), array("Content-Type: application/json", "Zotero-Write-Token: {$token}")); $this->assert412($response); }
private function _testMultiObjectLastModifiedVersion($objectType) { $objectTypePlural = API::getPluralObjectType($objectType); $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?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}", 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}?limit=1"); $this->assertEquals($version, $response->getHeader("Last-Modified-Version")); // Create a new object, using library timestamp $response = API::userPost(self::$config['userID'], "{$objectTypePlural}", json_encode([$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}"); $this->assert200($response); $version = $response->getHeader("Last-Modified-Version"); $this->assertTrue(is_numeric($version)); $this->assertEquals($version, $version2); $json = API::getJSONFromResponse($response)['data']; // Modify object $json['key'] = $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['version']); $response = API::userPost(self::$config['userID'], "{$objectTypePlural}", json_encode([$json]), array("Content-Type: application/json")); $this->assert428ForObject($response); // Outdated object version property $json['version'] = $version - 1; $response = API::userPost(self::$config['userID'], "{$objectTypePlural}", json_encode([$json]), array("Content-Type: application/json")); $this->assert412ForObject($response, ucwords($objectType) . " has been modified since specified version " . "(expected {$json['version']}, found {$version})"); // Modify object, using object version property $json['version'] = $version; $response = API::userPost(self::$config['userID'], "{$objectTypePlural}", json_encode([$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}"); $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}"); $version = $response->getHeader("Last-Modified-Version"); $this->assertTrue(is_numeric($version)); $this->assertEquals($version, $version3); // TODO: Version should be incremented on deleted item }
public function testNewInvalidBookItem() { $json = API::getItemTemplate("book"); // Missing item type $json2 = clone $json; unset($json2->itemType); $response = API::userPost( self::$config['userID'], "items", json_encode([$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", json_encode([$json2]), array("Content-Type: application/json") ); $this->assert400ForObject($response, "'contentType' is valid only for attachment items"); // more tests }
public function testLinkedFileAttachment() { $msg = "Linked-file attachments cannot be added to publications libraries"; // Create top-level item API::useAPIKey(self::$config['apiKey']); $json = API::getItemTemplate("book"); $response = API::userPost(self::$config['userID'], "publications/items", json_encode([$json])); $this->assert200($response); $json = API::getJSONFromResponse($response); $itemKey = $json['success'][0]; $json = API::getItemTemplate("attachment&linkMode=linked_file"); $json->parentItem = $itemKey; API::useAPIKey(self::$config['apiKey']); $response = API::userPost(self::$config['userID'], "publications/items", json_encode([$json]), array("Content-Type: application/json")); $this->assert400ForObject($response, $msg, 0); }
public function testRelatedItemRelationsSingleRequest() { $uriPrefix = "http://zotero.org/users/" . self::$config['userID'] . "/items/"; // TEMP: Use autoloader 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->key = $item1Key; $item1JSON->version = 0; $item1JSON->relations->{'dc:relation'} = $item2URI; $item2JSON = API::getItemTemplate('book'); $item2JSON->key = $item2Key; $item2JSON->version = 0; $response = API::postItems([$item1JSON, $item2JSON]); $this->assert200($response); $json = API::getJSONFromResponse($response); // Make sure it exists on item 1 $json = API::getItem($item1JSON->key, $this, 'json')['data']; $this->assertCount(1, $json['relations']); $this->assertEquals($item2URI, $json['relations']['dc:relation']); // And item 2, since related items are bidirectional $json = API::getItem($item2JSON->key, $this, 'json')['data']; $this->assertCount(1, $json['relations']); $this->assertEquals($item1URI, $json['relations']['dc:relation']); }
public function testPatchItems() { // Create top-level item API::useAPIKey(self::$config['apiKey']); $json = API::getItemTemplate("book"); $response = API::userPost(self::$config['userID'], "publications/items", json_encode([$json])); $this->assert200($response); $key = API::getJSONFromResponse($response)['successful'][0]['key']; $version = $response->getHeader("Last-Modified-Version"); $json = ["key" => $key, "version" => $version, "title" => "Test"]; $response = API::userPost(self::$config['userID'], "publications/items", json_encode([$json]), ["Content-Type: application/json"]); $this->assert200ForObject($response); }
private function _testPartialWriteFailureWithUnchanged($objectType) { API::userClear(self::$config['userID']); $objectTypePlural = API::getPluralObjectType($objectType); switch ($objectType) { case 'collection': $json1 = API::createCollection("Test", false, $this, 'jsonData'); $json2 = array("name" => str_repeat("1234567890", 6554)); $json3 = array("name" => "Test"); break; case 'item': $json1 = API::createItem("book", array("title" => "Title"), $this, 'jsonData'); $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')); $json1 = API::createSearch("Name", $conditions, $this, 'jsonData'); $json2 = array("name" => str_repeat("1234567890", 6554), "conditions" => $conditions); $json3 = array("name" => "Test", "conditions" => $conditions); break; } $response = API::userPost(self::$config['userID'], "{$objectTypePlural}", json_encode([$json1, $json2, $json3]), array("Content-Type: application/json")); $this->assert200($response); $json = API::getJSONFromResponse($response); $this->assertUnchangedForObject($response, 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); } }