Exemplo n.º 1
0
 public function testTagDeletePermissions()
 {
     API::userClear(self::$config['userID']);
     $xml = API::createItem('book', array("tags" => array(array("tag" => "A"))), $this);
     $libraryVersion = API::getLibraryVersion();
     API::setKeyOption(self::$config['userID'], self::$config['apiKey'], 'libraryWrite', 0);
     $response = API::userDelete(self::$config['userID'], "tags?tag=A&key=" . self::$config['apiKey']);
     $this->assert403($response);
     API::setKeyOption(self::$config['userID'], self::$config['apiKey'], 'libraryWrite', 1);
     $response = API::userDelete(self::$config['userID'], "tags?tag=A&key=" . self::$config['apiKey'], array("If-Unmodified-Since-Version: {$libraryVersion}"));
     $this->assert204($response);
 }
Exemplo n.º 2
0
 public function testIsReplacedBy()
 {
     $xml = Sync::updated(self::$sessionID);
     $updateKey = $xml['updateKey'];
     $lastSyncTimestamp = $xml['timestamp'];
     $key1 = 'AAAAAAAA';
     $uri1 = "http://zotero.org/users/" . self::$config['userID'] . '/items/' . $key1;
     $data = API::createItem("journalArticle", array("relations" => array("dc:replaces" => $uri1)), null, 'data');
     $key2 = $data['key'];
     $libraryVersion = $data['version'];
     $uri2 = "http://zotero.org/users/" . self::$config['userID'] . '/items/' . $key2;
     $xml = Sync::updated(self::$sessionID);
     // For classic sync, dc:replaces should be swapped for dc:isReplacedBy
     $this->assertEquals($uri1, (string) $xml->updated[0]->relations->relation[0]->subject);
     $this->assertEquals("dc:isReplacedBy", (string) $xml->updated[0]->relations->relation[0]->predicate);
     $this->assertEquals($uri2, (string) $xml->updated[0]->relations->relation[0]->object);
     $response = API::userDelete(self::$config['userID'], "items/{$key2}?key=" . self::$config['apiKey'], array("If-Unmodified-Since-Version: {$libraryVersion}"));
     $this->assertEquals(204, $response->getStatus());
     $xml = Sync::updated(self::$sessionID);
     $this->assertEquals(1, $xml->updated[0]->relations->count());
     $this->assertEquals(1, $xml->updated[0]->deleted[0]->items[0]->item->count());
     $this->assertEquals(0, $xml->updated[0]->deleted[0]->items[0]->relations->count());
 }
Exemplo n.º 3
0
 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;
     }
     // 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}?key=" . self::$config['apiKey'] . "&content=json");
     $this->assert200($response);
     $objectVersion = $response->getHeader("Last-Modified-Version");
     $xml = API::getXMLFromResponse($response);
     $data = API::parseDataFromAtomEntry($xml);
     $json = json_decode($data['content']);
     $this->assertEquals($objectVersion, $json->{$versionProp});
     $this->assertEquals($objectVersion, $data['version']);
     $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'] . "&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->{$versionProp});
     $response = API::userPut(self::$config['userID'], "{$objectTypePlural}/{$objectKey}?key=" . self::$config['apiKey'], json_encode($json));
     $this->assert428($response);
     // Out of date version
     $response = API::userPut(self::$config['userID'], "{$objectTypePlural}/{$objectKey}?key=" . self::$config['apiKey'], 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}?key=" . self::$config['apiKey'], 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->{$versionProp} = $newObjectVersion;
     $response = API::userPut(self::$config['userID'], "{$objectTypePlural}/{$objectKey}?key=" . self::$config['apiKey'], 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}?key=" . self::$config['apiKey'] . "&limit=1");
     $this->assert200($response);
     $newLibraryVersion = $response->getHeader("Last-Modified-Version");
     $this->assertEquals($newObjectVersion2, $newLibraryVersion);
     // 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}?key=" . self::$config['apiKey'] . "&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}?key=" . self::$config['apiKey']);
     $this->assert428($response);
     // Outdated If-Unmodified-Since-Version
     $response = API::userDelete(self::$config['userID'], "{$objectTypePlural}/{$objectKey}?key=" . self::$config['apiKey'], array("If-Unmodified-Since-Version: " . $objectVersion));
     $this->assert412($response);
     // Delete object
     $response = API::userDelete(self::$config['userID'], "{$objectTypePlural}/{$objectKey}?key=" . self::$config['apiKey'], array("If-Unmodified-Since-Version: " . $newObjectVersion2));
     $this->assert204($response);
 }
Exemplo n.º 4
0
 public function testMultiTagDelete()
 {
     $tags1 = array("a", "aa", "b");
     $tags2 = array("b", "c", "cc");
     $tags3 = array("Foo");
     API::createItem("book", array("tags" => array_map(function ($tag) {
         return array("tag" => $tag);
     }, $tags1)), $this, 'key');
     API::createItem("book", array("tags" => array_map(function ($tag) {
         return array("tag" => $tag, "type" => 1);
     }, $tags2)), $this, 'key');
     API::createItem("book", array("tags" => array_map(function ($tag) {
         return array("tag" => $tag);
     }, $tags3)), $this, 'key');
     $libraryVersion = API::getLibraryVersion();
     // Missing version header
     $response = API::userDelete(self::$config['userID'], "tags?key=" . self::$config['apiKey'] . "&content=json&tag=" . implode("%20||%20", array_merge($tags1, $tags2)));
     $this->assert428($response);
     // Outdated version header
     $response = API::userDelete(self::$config['userID'], "tags?key=" . self::$config['apiKey'] . "&content=json&tag=" . implode("%20||%20", array_merge($tags1, $tags2)), array("If-Unmodified-Since-Version: " . ($libraryVersion - 1)));
     $this->assert412($response);
     // Delete
     $response = API::userDelete(self::$config['userID'], "tags?key=" . self::$config['apiKey'] . "&content=json&tag=" . implode("%20||%20", array_merge($tags1, $tags2)), array("If-Unmodified-Since-Version: {$libraryVersion}"));
     $this->assert204($response);
     // Make sure they're gone
     $response = API::userGet(self::$config['userID'], "tags?key=" . self::$config['apiKey'] . "&content=json&tag=" . implode("%20||%20", array_merge($tags1, $tags2, $tags3)));
     $this->assert200($response);
     $this->assertNumResults(1, $response);
 }
Exemplo n.º 5
0
 public function testDeleteNonexistentSetting()
 {
     $response = API::userDelete(self::$config['userID'], "settings/nonexistentSetting?key=" . self::$config['apiKey'], array("If-Unmodified-Since-Version: 0"));
     $this->assert404($response);
 }
Exemplo n.º 6
0
 private function _testMultiObjectDelete($objectType)
 {
     $objectTypePlural = API::getPluralObjectType($objectType);
     $keyProp = $objectType . "Key";
     $deleteKeys = array();
     $keepKeys = array();
     switch ($objectType) {
         case 'collection':
             $deleteKeys[] = API::createCollection("Name", false, $this, 'key');
             $deleteKeys[] = API::createCollection("Name", false, $this, 'key');
             $keepKeys[] = API::createCollection("Name", false, $this, 'key');
             break;
         case 'item':
             $deleteKeys[] = API::createItem("book", array("title" => "Title"), $this, 'key');
             $deleteKeys[] = API::createItem("book", array("title" => "Title"), $this, 'key');
             $keepKeys[] = API::createItem("book", array("title" => "Title"), $this, 'key');
             break;
         case 'search':
             $deleteKeys[] = API::createSearch("Name", 'default', $this, 'key');
             $deleteKeys[] = API::createSearch("Name", 'default', $this, 'key');
             $keepKeys[] = API::createSearch("Name", 'default', $this, 'key');
             break;
     }
     $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey']);
     $this->assert200($response);
     $this->assertNumResults(sizeOf($deleteKeys) + sizeOf($keepKeys), $response);
     $libraryVersion = $response->getHeader("Last-Modified-Version");
     $response = API::userDelete(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'] . "&{$keyProp}=" . implode(',', $deleteKeys), array("If-Unmodified-Since-Version: " . $libraryVersion));
     $this->assert204($response);
     $libraryVersion = $response->getHeader("Last-Modified-Version");
     $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey']);
     $this->assert200($response);
     $this->assertNumResults(sizeOf($keepKeys), $response);
     $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'] . "&{$keyProp}=" . implode(',', $keepKeys));
     $this->assert200($response);
     $this->assertNumResults(sizeOf($keepKeys), $response);
     // Add trailing comma to itemKey param, to test key parsing
     $response = API::userDelete(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'] . "&{$keyProp}=" . implode(',', $keepKeys) . ",", array("If-Unmodified-Since-Version: " . $libraryVersion));
     $this->assert204($response);
     $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey']);
     $this->assert200($response);
     $this->assertNumResults(0, $response);
 }