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