예제 #1
0
 private function _testUploadUnmodified($objectType)
 {
     $objectTypePlural = API::getPluralObjectType($objectType);
     switch ($objectType) {
         case 'collection':
             $data = API::createCollection("Name", false, $this, 'jsonData');
             break;
         case 'item':
             $data = API::createItem("book", array("title" => "Title"), $this, 'jsonData');
             break;
         case 'search':
             $data = API::createSearch("Name", 'default', $this, 'jsonData');
             break;
     }
     $version = $data['version'];
     $this->assertNotEquals(0, $version);
     $response = API::userPut(self::$config['userID'], "{$objectTypePlural}/{$data['key']}", json_encode($data));
     $this->assert204($response);
     $this->assertEquals($version, $response->getHeader("Last-Modified-Version"));
     switch ($objectType) {
         case 'collection':
             $json = API::getCollection($data['key'], $this, 'json');
             break;
         case 'item':
             $json = API::getItem($data['key'], $this, 'json');
             break;
         case 'search':
             $json = API::getSearch($data['key'], $this, 'json');
             break;
     }
     $this->assertEquals($version, $json['version']);
 }
예제 #2
0
	public function testTop() {
		API::userClear(self::$config['userID']);
		
		$collectionKey = API::createCollection('Test', false, $this, 'key');
		$emptyCollectionKey = API::createCollection('Empty', 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, JSON
		$response = API::userGet(
			self::$config['userID'],
			"items/top"
		);
		$this->assert200($response);
		$this->assertNumResults(sizeOf($parentKeys), $response);
		$json = API::getJSONFromResponse($response);
		$done = [];
		foreach ($json as $item) {
			$this->assertContains($item['key'], $parentKeys);
			$this->assertNotContains($item['key'], $done);
			$done[] = $item['key'];
		}
		
		// /top, Atom
		$response = API::userGet(
			self::$config['userID'],
			"items/top?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, JSON, in collection
		$response = API::userGet(
			self::$config['userID'],
			"collections/$collectionKey/items/top"
		);
		$this->assert200($response);
		$this->assertNumResults(1, $response);
		$json = API::getJSONFromResponse($response);
		$this->assertCount(1, $json);
		$this->assertEquals($parentKeys[0], $json[0]['key']);
		
		// /top, Atom, in collection
		$response = API::userGet(
			self::$config['userID'],
			"collections/$collectionKey/items/top?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, JSON, in empty collection
		$response = API::userGet(
			self::$config['userID'],
			"collections/$emptyCollectionKey/items/top"
		);
		$this->assert200($response);
		$this->assertNumResults(0, $response);
		$this->assertTotalResults(0, $response);
		
		// /top, keys
		$response = API::userGet(
			self::$config['userID'],
			"items/top?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?format=keys"
		);
		$this->assert200($response);
		$this->assertEquals($parentKeys[0], trim($response->getBody()));
		
		// /top with itemKey for parent, JSON
		$response = API::userGet(
			self::$config['userID'],
			"items/top?itemKey=" . $parentKeys[0]
		);
		$this->assert200($response);
		$this->assertNumResults(1, $response);
		$json = API::getJSONFromResponse($response);
		$this->assertEquals($parentKeys[0], $json[0]['key']);
		
		// /top with itemKey for parent, Atom
		$response = API::userGet(
			self::$config['userID'],
			"items/top?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, JSON, in collection
		$response = API::userGet(
			self::$config['userID'],
			"collections/$collectionKey/items/top?itemKey=" . $parentKeys[0]
		);
		$this->assert200($response);
		$this->assertNumResults(1, $response);
		$json = API::getJSONFromResponse($response);
		$this->assertEquals($parentKeys[0], $json[0]['key']);
		
		// /top with itemKey for parent, Atom, in collection
		$response = API::userGet(
			self::$config['userID'],
			"collections/$collectionKey/items/top?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?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?format=keys&itemKey=" . $parentKeys[0]
		);
		$this->assert200($response);
		$this->assertEquals($parentKeys[0], trim($response->getBody()));
		
		// /top with itemKey for child, JSON
		$response = API::userGet(
			self::$config['userID'],
			"items/top?itemKey=" . $childKeys[0]
		);
		$this->assert200($response);
		$this->assertNumResults(1, $response);
		$json = API::getJSONFromResponse($response);
		$this->assertEquals($parentKeys[0], $json[0]['key']);
		
		// /top with itemKey for child, Atom
		$response = API::userGet(
			self::$config['userID'],
			"items/top?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?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?q=$parentTitleSearch"
		);
		$this->assert200($response);
		$this->assertNumResults(sizeOf($parentKeys), $response);
		$json = API::getJSONFromResponse($response);
		$done = [];
		foreach ($json as $item) {
			$this->assertContains($item['key'], $parentKeys);
			$this->assertNotContains($item['key'], $done);
			$done[] = $item['key'];
		}
		
		// /top, Atom, with q for all items
		$response = API::userGet(
			self::$config['userID'],
			"items/top?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, JSON, in collection, with q for all items
		$response = API::userGet(
			self::$config['userID'],
			"collections/$collectionKey/items/top?q=$parentTitleSearch"
		);
		$this->assert200($response);
		$this->assertNumResults(1, $response);
		$json = API::getJSONFromResponse($response);
		$this->assertContains($parentKeys[0], $json[0]['key']);
		
		// /top, Atom, in collection, with q for all items
		$response = API::userGet(
			self::$config['userID'],
			"collections/$collectionKey/items/top?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, JSON, with q for child item
		$response = API::userGet(
			self::$config['userID'],
			"items/top?q=$childTitleSearch"
		);
		$this->assert200($response);
		$this->assertNumResults(1, $response);
		$json = API::getJSONFromResponse($response);
		$this->assertContains($parentKeys[0], $json[0]['key']);
		
		// /top, Atom, with q for child item
		$response = API::userGet(
			self::$config['userID'],
			"items/top?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, JSON, in collection, with q for child item
		$response = API::userGet(
			self::$config['userID'],
			"collections/$collectionKey/items/top?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, in collection, with q for child item
		$response = API::userGet(
			self::$config['userID'],
			"collections/$collectionKey/items/top?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, JSON, with q for all items, ordered by title
		$response = API::userGet(
			self::$config['userID'],
			"items/top?q=$parentTitleSearch"
				. "&order=title"
		);
		$this->assert200($response);
		$this->assertNumResults(sizeOf($parentKeys), $response);
		$json = API::getJSONFromResponse($response);
		$returnedTitles = [];
		foreach ($json as $item) {
			$returnedTitles[] = $item['data']['title'];
		}
		$orderedTitles = [$parentTitle1, $parentTitle2, $parentTitle3];
		sort($orderedTitles);
		$this->assertEquals($orderedTitles, $returnedTitles);
		
		// /top, Atom, with q for all items, ordered by title
		$response = API::userGet(
			self::$config['userID'],
			"items/top?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?q=$parentTitleSearch"
				. "&order=date&sort=asc"
		);
		$this->assert200($response);
		$this->assertNumResults(sizeOf($parentKeys), $response);
		$json = API::getJSONFromResponse($response);
		$orderedResults = array_map(function ($val) {
			return $val['data']['date'];
		}, $json);
		$this->assertEquals($orderedDates, $orderedResults);
		
		// /top, Atom, with q for all items, ordered by date asc
		$response = API::userGet(
			self::$config['userID'],
			"items/top?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, JSON, with q for all items, ordered by date desc
		$response = API::userGet(
			self::$config['userID'],
			"items/top?q=$parentTitleSearch"
				. "&order=date&sort=desc"
		);
		$this->assert200($response);
		$this->assertNumResults(sizeOf($parentKeys), $response);
		$json = API::getJSONFromResponse($response);
		$orderedDatesReverse = array_reverse($orderedDates);
		$orderedResults = array_map(function ($val) {
			return $val['data']['date'];
		}, $json);
		$this->assertEquals($orderedDatesReverse, $orderedResults);
		
		// /top, Atom, with q for all items, ordered by date desc
		$response = API::userGet(
			self::$config['userID'],
			"items/top?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?q=$parentTitleSearch"
				. "&order=itemType"
		);
		$this->assert200($response);
		$this->assertNumResults(sizeOf($parentKeys), $response);
		$json = API::getJSONFromResponse($response);
		$orderedItemTypes = $itemTypes;
		sort($orderedItemTypes);
		$orderedResults = array_map(function ($val) {
			return $val['data']['itemType'];
		}, $json);
		$this->assertEquals($orderedItemTypes, $orderedResults);
		
		// /top, Atom, with q for all items, ordered by item type asc
		$response = API::userGet(
			self::$config['userID'],
			"items/top?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?q=$parentTitleSearch"
				. "&order=itemType&sort=desc"
		);
		$this->assert200($response);
		$this->assertNumResults(sizeOf($parentKeys), $response);
		$json = API::getJSONFromResponse($response);
		$orderedItemTypes = $itemTypes;
		rsort($orderedItemTypes);
		$orderedResults = array_map(function ($val) {
			return $val['data']['itemType'];
		}, $json);
		$this->assertEquals($orderedItemTypes, $orderedResults);
		
		// /top, Atom, with q for all items, ordered by item type desc
		$response = API::userGet(
			self::$config['userID'],
			"items/top?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);
	}
예제 #3
0
 public function testDeleteCollectionRelation()
 {
     $relations = array("owl:sameAs" => "http://zotero.org/groups/1/collections/AAAAAAAA");
     $data = API::createCollection("Test", array("relations" => $relations), $this, 'jsonData');
     // Remove all relations
     $data['relations'] = new \stdClass();
     unset($relations['owl:sameAs']);
     $response = API::userPut(self::$config['userID'], "collections/{$data['key']}", json_encode($data));
     $this->assert204($response);
     // Make sure it's gone
     $data = API::getCollection($data['key'], $this, 'json')['data'];
     $this->assertCount(sizeOf($relations), $data['relations']);
     foreach ($relations as $predicate => $object) {
         $this->assertEquals($object, $data['relations'][$predicate]);
     }
 }
예제 #4
0
 public function testCollectionItems()
 {
     $collectionKey = API::createCollection('Test', false, $this, 'key');
     $json = API::createItem("book", ['collections' => [$collectionKey]], $this, 'jsonData');
     $itemKey1 = $json['key'];
     $itemVersion1 = $json['version'];
     $this->assertEquals([$collectionKey], $json['collections']);
     $json = API::createItem("journalArticle", ['collections' => [$collectionKey]], $this, 'jsonData');
     $itemKey2 = $json['key'];
     $itemVersion2 = $json['version'];
     $this->assertEquals([$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?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?format=keys");
     $this->assert200($response);
     $keys = explode("\n", trim($response->getBody()));
     $this->assertCount(2, $keys);
     $this->assertContains($itemKey1, $keys);
     $this->assertContains($itemKey2, $keys);
 }
예제 #5
0
 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?q=another");
     $this->assert200($response);
     $this->assertNumResults(1, $response);
     $json = API::getJSONFromResponse($response);
     $this->assertEquals($keys[1], $json[0]['key']);
     // No results
     $response = API::userGet(self::$config['userID'], "collections?q=nothing");
     $this->assert200($response);
     $this->assertNumResults(0, $response);
 }
예제 #6
0
 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);
     }
 }