public function testChangeItemType() { $json = API::getItemTemplate("book"); $json->title = "Foo"; $json->numPages = 100; $response = API::userPost( self::$config['userID'], "items", json_encode([$json]), array("Content-Type: application/json") ); $key = API::getFirstSuccessKeyFromResponse($response); $json1 = API::getItem($key, $this, 'json')['data']; $version = $json1['version']; $json2 = API::getItemTemplate("bookSection"); foreach ($json2 as $field => &$val) { if ($field != "itemType" && isset($json1[$field])) { $val = $json1[$field]; } } $response = API::userPut( self::$config['userID'], "items/$key", json_encode($json2), array( "Content-Type: application/json", "If-Unmodified-Since-Version: $version" ) ); $this->assert204($response); $json = API::getItem($key, $this, 'json')['data']; $this->assertEquals("bookSection", $json['itemType']); $this->assertEquals("Foo", $json['title']); $this->assertArrayNotHasKey("numPages", $json); }
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 testKeyNoteAccess() { API::userClear(self::$config['userID']); API::setKeyOption(self::$config['userID'], self::$config['apiKey'], 'libraryNotes', 1); $keys = array(); $topLevelKeys = array(); $bookKeys = array(); $key = API::createItem('book', array("title" => "A"), $this, 'key'); $keys[] = $key; $topKeys[] = $key; $bookKeys[] = $key; $key = API::createNoteItem("B", false, $this, 'key'); $keys[] = $key; $topKeys[] = $key; $key = API::createNoteItem("C", false, $this, 'key'); $keys[] = $key; $topKeys[] = $key; $key = API::createNoteItem("D", false, $this, 'key'); $keys[] = $key; $topKeys[] = $key; $key = API::createNoteItem("E", false, $this, 'key'); $keys[] = $key; $topKeys[] = $key; $key = API::createItem('book', array("title" => "F"), $this, 'key'); $keys[] = $key; $topKeys[] = $key; $bookKeys[] = $key; $key = API::createNoteItem("G", $key, $this, 'key'); $keys[] = $key; // Create collection and add items to it $response = API::userPost(self::$config['userID'], "collections", json_encode([["name" => "Test", "parentCollection" => false]]), array("Content-Type: application/json")); $this->assert200ForObject($response); $collectionKey = API::getFirstSuccessKeyFromResponse($response); $response = API::userPost(self::$config['userID'], "collections/{$collectionKey}/items", implode(" ", $topKeys)); $this->assert204($response); // // format=atom // // Root $response = API::userGet(self::$config['userID'], "items"); $this->assertNumResults(sizeOf($keys), $response); $this->assertTotalResults(sizeOf($keys), $response); // Top $response = API::userGet(self::$config['userID'], "items/top"); $this->assertNumResults(sizeOf($topKeys), $response); $this->assertTotalResults(sizeOf($topKeys), $response); // Collection $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items/top"); $this->assertNumResults(sizeOf($topKeys), $response); $this->assertTotalResults(sizeOf($topKeys), $response); // // format=keys // // Root $response = API::userGet(self::$config['userID'], "items?format=keys"); $this->assert200($response); $this->assertCount(sizeOf($keys), explode("\n", trim($response->getBody()))); // Top $response = API::userGet(self::$config['userID'], "items/top?format=keys"); $this->assert200($response); $this->assertCount(sizeOf($topKeys), explode("\n", trim($response->getBody()))); // Collection $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items/top?format=keys"); $this->assert200($response); $this->assertCount(sizeOf($topKeys), explode("\n", trim($response->getBody()))); // Remove notes privilege from key API::setKeyOption(self::$config['userID'], self::$config['apiKey'], 'libraryNotes', 0); // // format=json // // totalResults with limit $response = API::userGet(self::$config['userID'], "items?limit=1"); $this->assertNumResults(1, $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // And without limit $response = API::userGet(self::$config['userID'], "items"); $this->assertNumResults(sizeOf($bookKeys), $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // Top $response = API::userGet(self::$config['userID'], "items/top"); $this->assertNumResults(sizeOf($bookKeys), $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // Collection $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items"); $this->assertNumResults(sizeOf($bookKeys), $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // // format=atom // // totalResults with limit $response = API::userGet(self::$config['userID'], "items?format=atom&limit=1"); $this->assertNumResults(1, $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // And without limit $response = API::userGet(self::$config['userID'], "items?format=atom"); $this->assertNumResults(sizeOf($bookKeys), $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // Top $response = API::userGet(self::$config['userID'], "items/top?format=atom"); $this->assertNumResults(sizeOf($bookKeys), $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // Collection $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items?format=atom"); $this->assertNumResults(sizeOf($bookKeys), $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // // format=keys // $response = API::userGet(self::$config['userID'], "items?format=keys"); $keys = explode("\n", trim($response->getBody())); sort($keys); $this->assertEmpty(array_merge(array_diff($bookKeys, $keys), array_diff($keys, $bookKeys))); }