public function testTop() { API::userClear(self::$config['userID']); $collectionKey = API::createCollection('Test', false, $this, 'key'); $parentTitle1 = "Parent Title"; $childTitle1 = "This is a Test Title"; $parentTitle2 = "Another Parent Title"; $parentTitle3 = "Yet Another Parent Title"; $noteText = "This is a sample note."; $parentTitleSearch = "title"; $childTitleSearch = "test"; $dates = ["2013", "January 3, 2010", ""]; $orderedDates = [$dates[2], $dates[1], $dates[0]]; $itemTypes = ["journalArticle", "newspaperArticle", "book"]; $parentKeys = []; $childKeys = []; $parentKeys[] = API::createItem($itemTypes[0], ['title' => $parentTitle1, 'date' => $dates[0], 'collections' => [$collectionKey]], $this, 'key'); $childKeys[] = API::createAttachmentItem("linked_url", ['title' => $childTitle1], $parentKeys[0], $this, 'key'); $parentKeys[] = API::createItem($itemTypes[1], ['title' => $parentTitle2, 'date' => $dates[1]], $this, 'key'); $childKeys[] = API::createNoteItem($noteText, $parentKeys[1], $this, 'key'); // Create item with deleted child that matches child title search $parentKeys[] = API::createItem($itemTypes[2], ['title' => $parentTitle3], $this, 'key'); API::createAttachmentItem("linked_url", ['title' => $childTitle1, 'deleted' => true], $parentKeys[sizeOf($parentKeys) - 1], $this, 'key'); // Add deleted item with non-deleted child $deletedKey = API::createItem("book", ['title' => "This is a deleted item", 'deleted' => true], $this, 'key'); API::createNoteItem("This is a child note of a deleted item.", $deletedKey, $this, 'key'); // /top, Atom $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&content=json"); $this->assert200($response); $this->assertNumResults(sizeOf($parentKeys), $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:key'); $this->assertCount(sizeOf($parentKeys), $xpath); foreach ($parentKeys as $parentKey) { $this->assertContains($parentKey, $xpath); } // /top, Atom, in collection $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items/top?key=" . self::$config['apiKey'] . "&content=json"); $this->assert200($response); $this->assertNumResults(1, $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:key'); $this->assertCount(1, $xpath); $this->assertContains($parentKeys[0], $xpath); // /top, keys $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&format=keys"); $this->assert200($response); $keys = explode("\n", trim($response->getBody())); $this->assertCount(sizeOf($parentKeys), $keys); foreach ($parentKeys as $parentKey) { $this->assertContains($parentKey, $keys); } // /top, keys, in collection $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items/top?key=" . self::$config['apiKey'] . "&format=keys"); $this->assert200($response); $this->assertEquals($parentKeys[0], trim($response->getBody())); // /top with itemKey for parent, Atom $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&content=json&itemKey=" . $parentKeys[0]); $this->assert200($response); $this->assertNumResults(1, $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:key'); $this->assertEquals($parentKeys[0], (string) array_shift($xpath)); // /top with itemKey for parent, Atom, in collection $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items/top?key=" . self::$config['apiKey'] . "&content=json&itemKey=" . $parentKeys[0]); $this->assert200($response); $this->assertNumResults(1, $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:key'); $this->assertEquals($parentKeys[0], (string) array_shift($xpath)); // /top with itemKey for parent, keys $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&format=keys&itemKey=" . $parentKeys[0]); $this->assert200($response); $this->assertEquals($parentKeys[0], trim($response->getBody())); // /top with itemKey for parent, keys, in collection $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items/top?key=" . self::$config['apiKey'] . "&format=keys&itemKey=" . $parentKeys[0]); $this->assert200($response); $this->assertEquals($parentKeys[0], trim($response->getBody())); // /top with itemKey for child, Atom $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&content=json&itemKey=" . $childKeys[0]); $this->assert200($response); $this->assertNumResults(1, $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:key'); $this->assertEquals($parentKeys[0], (string) array_shift($xpath)); // /top with itemKey for child, keys $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&format=keys&itemKey=" . $childKeys[0]); $this->assert200($response); $this->assertEquals($parentKeys[0], trim($response->getBody())); // /top, Atom, with q for all items $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&content=json&q={$parentTitleSearch}"); $this->assert200($response); $this->assertNumResults(sizeOf($parentKeys), $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:key'); $this->assertCount(sizeOf($parentKeys), $xpath); foreach ($parentKeys as $parentKey) { $this->assertContains($parentKey, $xpath); } // /top, Atom, in collection, with q for all items $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items/top?key=" . self::$config['apiKey'] . "&content=json&q={$parentTitleSearch}"); $this->assert200($response); $this->assertNumResults(1, $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:key'); $this->assertCount(1, $xpath); $this->assertContains($parentKeys[0], $xpath); // /top, Atom, with q for child item $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&content=json&q={$childTitleSearch}"); $this->assert200($response); $this->assertNumResults(1, $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:key'); $this->assertCount(1, $xpath); $this->assertContains($parentKeys[0], $xpath); // /top, Atom, in collection, with q for child item $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items/top?key=" . self::$config['apiKey'] . "&content=json&q={$childTitleSearch}"); $this->assert200($response); $this->assertNumResults(0, $response); // Not currently possible /*$this->assertNumResults(1, $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:key'); $this->assertCount(1, $xpath); $this->assertContains($parentKeys[0], $xpath);*/ // /top, Atom, with q for all items, ordered by title $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&content=json&q={$parentTitleSearch}" . "&order=title"); $this->assert200($response); $this->assertNumResults(sizeOf($parentKeys), $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/atom:title'); $this->assertCount(sizeOf($parentKeys), $xpath); $orderedTitles = [$parentTitle1, $parentTitle2, $parentTitle3]; sort($orderedTitles); $orderedResults = array_map(function ($val) { return (string) $val; }, $xpath); $this->assertEquals($orderedTitles, $orderedResults); // /top, Atom, with q for all items, ordered by date asc $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&content=json&q={$parentTitleSearch}" . "&order=date&sort=asc"); $this->assert200($response); $this->assertNumResults(sizeOf($parentKeys), $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/atom:content'); $this->assertCount(sizeOf($parentKeys), $xpath); $orderedResults = array_map(function ($val) { return json_decode($val)->date; }, $xpath); $this->assertEquals($orderedDates, $orderedResults); // /top, Atom, with q for all items, ordered by date desc $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&content=json&q={$parentTitleSearch}" . "&order=date&sort=desc"); $this->assert200($response); $this->assertNumResults(sizeOf($parentKeys), $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/atom:content'); $this->assertCount(sizeOf($parentKeys), $xpath); $orderedDatesReverse = array_reverse($orderedDates); $orderedResults = array_map(function ($val) { return json_decode($val)->date; }, $xpath); $this->assertEquals($orderedDatesReverse, $orderedResults); // /top, Atom, with q for all items, ordered by item type asc $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&content=json&q={$parentTitleSearch}" . "&order=itemType"); $this->assert200($response); $this->assertNumResults(sizeOf($parentKeys), $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:itemType'); $this->assertCount(sizeOf($parentKeys), $xpath); $orderedItemTypes = $itemTypes; sort($orderedItemTypes); $orderedResults = array_map(function ($val) { return (string) $val; }, $xpath); $this->assertEquals($orderedItemTypes, $orderedResults); // /top, Atom, with q for all items, ordered by item type desc $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&content=json&q={$parentTitleSearch}" . "&order=itemType&sort=desc"); $this->assert200($response); $this->assertNumResults(sizeOf($parentKeys), $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:itemType'); $this->assertCount(sizeOf($parentKeys), $xpath); $orderedItemTypes = $itemTypes; rsort($orderedItemTypes); $orderedResults = array_map(function ($val) { return (string) $val; }, $xpath); $this->assertEquals($orderedItemTypes, $orderedResults); }
public function testDeleteCollectionRelation() { $relations = array("owl:sameAs" => "http://zotero.org/groups/1/collections/AAAAAAAA"); $data = API::createCollection("Test", array("relations" => $relations), $this, 'data'); $json = json_decode($data['content'], true); // Remove all relations $json['relations'] = new \stdClass(); unset($relations['owl:sameAs']); $response = API::userPut(self::$config['userID'], "collections/{$data['key']}?key=" . self::$config['apiKey'], json_encode($json)); $this->assert204($response); // Make sure it's gone $xml = API::getCollectionXML($data['key']); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content'], true); $this->assertCount(sizeOf($relations), $json['relations']); foreach ($relations as $predicate => $object) { $this->assertEquals($object, $json['relations'][$predicate]); } }
public function testCollectionQuickSearch() { $title1 = "Test Title"; $title2 = "Another Title"; $keys = []; $keys[] = API::createCollection($title1, [], $this, 'key'); $keys[] = API::createCollection($title2, [], $this, 'key'); // Search by title $response = API::userGet(self::$config['userID'], "collections?key=" . self::$config['apiKey'] . "&content=json&q=another"); $this->assert200($response); $this->assertNumResults(1, $response); $xml = API::getXMLFromResponse($response); $xpath = $xml->xpath('//atom:entry/zapi:key'); $key = (string) array_shift($xpath); $this->assertEquals($keys[1], $key); // No results $response = API::userGet(self::$config['userID'], "collections?key=" . self::$config['apiKey'] . "&content=json&q=nothing"); $this->assert200($response); $this->assertNumResults(0, $response); }
private function _testUploadUnmodified($objectType) { $objectTypePlural = API::getPluralObjectType($objectType); switch ($objectType) { case 'collection': $xml = API::createCollection("Name", false, $this); break; case 'item': $xml = API::createItem("book", array("title" => "Title"), $this); break; case 'search': $xml = API::createSearch("Name", 'default', $this); break; } $version = (int) array_shift($xml->xpath('//atom:entry/zapi:version')); $this->assertNotEquals(0, $version); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content']); $response = API::userPut(self::$config['userID'], "{$objectTypePlural}/{$data['key']}?key=" . self::$config['apiKey'], json_encode($json)); $this->assert204($response); $this->assertEquals($version, $response->getHeader("Last-Modified-Version")); switch ($objectType) { case 'collection': $xml = API::getCollectionXML($data['key']); break; case 'item': $xml = API::getItemXML($data['key']); break; case 'search': $xml = API::getSearchXML($data['key']); break; } $data = API::parseDataFromAtomEntry($xml); $this->assertEquals($version, $data['version']); }
public function testCollectionItems() { $collectionKey = API::createCollection('Test', false, $this, 'key'); $xml = API::createItem("book", array('collections' => array($collectionKey)), $this); $data = API::parseDataFromAtomEntry($xml); $itemKey1 = $data['key']; $itemVersion1 = $data['version']; $json = json_decode($data['content']); $this->assertEquals(array($collectionKey), $json->collections); $xml = API::createItem("journalArticle", array('collections' => array($collectionKey)), $this); $data = API::parseDataFromAtomEntry($xml); $itemKey2 = $data['key']; $itemVersion2 = $data['version']; $json = json_decode($data['content']); $this->assertEquals(array($collectionKey), $json->collections); $childItemKey1 = API::createAttachmentItem("linked_url", [], $itemKey1, $this, 'key'); $childItemKey2 = API::createAttachmentItem("linked_url", [], $itemKey2, $this, 'key'); $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items?key=" . self::$config['apiKey'] . "&format=keys"); $this->assert200($response); $keys = explode("\n", trim($response->getBody())); $this->assertCount(4, $keys); $this->assertContains($itemKey1, $keys); $this->assertContains($itemKey2, $keys); $this->assertContains($childItemKey1, $keys); $this->assertContains($childItemKey2, $keys); $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items/top?key=" . self::$config['apiKey'] . "&format=keys"); $this->assert200($response); $keys = explode("\n", trim($response->getBody())); $this->assertCount(2, $keys); $this->assertContains($itemKey1, $keys); $this->assertContains($itemKey2, $keys); }
public function testEditSingleCollection() { API::useAPIVersion(2); $xml = API::createCollection("Test", false, $this); $data = API::parseDataFromAtomEntry($xml); $key = $data['key']; $version = $data['version']; API::useAPIVersion(1); $xml = API::getCollectionXML($data['key']); $etag = (string) array_shift($xml->xpath('//atom:entry/atom:content/@etag')); $this->assertNotNull($etag); $newName = "Test 2"; $json = array("name" => $newName, "parent" => false); $response = API::userPut(self::$config['userID'], "collections/{$key}?key=" . self::$config['apiKey'], json_encode($json), array("Content-Type: application/json", "If-Match: {$etag}")); $this->assert200($response); $xml = API::getXMLFromResponse($response); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content']); $this->assertEquals($newName, (string) $json->name); }
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); } }