Beispiel #1
0
 public function testCreatorSummaryJSON()
 {
     $json = API::createItem("book", array("creators" => array(array("creatorType" => "author", "name" => "Test"))), $this, 'json');
     $itemKey = $json['key'];
     $this->assertEquals("Test", $json['meta']['creatorSummary']);
     $json = $json['data'];
     $json['creators'][] = ["creatorType" => "author", "firstName" => "Alice", "lastName" => "Foo"];
     $response = API::userPut(self::$config['userID'], "items/{$itemKey}", json_encode($json));
     $this->assert204($response);
     $json = API::getItem($itemKey, $this, 'json');
     $this->assertEquals("Test and Foo", $json['meta']['creatorSummary']);
     $json = $json['data'];
     $json['creators'][] = array("creatorType" => "author", "firstName" => "Bob", "lastName" => "Bar");
     $response = API::userPut(self::$config['userID'], "items/{$itemKey}", json_encode($json));
     $this->assert204($response);
     $json = API::getItem($itemKey, $this, 'json');
     $this->assertEquals("Test et al.", $json['meta']['creatorSummary']);
 }
Beispiel #2
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']);
 }
Beispiel #3
0
	public function testParentItemPatch() {
		$json = API::createItem("book", false, $this, 'jsonData');
		$parentKey = $json['key'];
		$parentVersion = $json['version'];
		
		$json = API::createAttachmentItem("linked_url", [], $parentKey, $this, 'jsonData');
		$childKey = $json['key'];
		$childVersion = $json['version'];
		
		$this->assertArrayHasKey('parentItem', $json);
		$this->assertEquals($parentKey, $json['parentItem']);
		
		$json = array(
			'title' => 'Test'
		);
		
		// With PATCH, parent shouldn't be removed even though unspecified
		$response = API::userPatch(
			self::$config['userID'],
			"items/$childKey",
			json_encode($json),
			array("If-Unmodified-Since-Version: " . $childVersion)
		);
		$this->assert204($response);
		
		$json = API::getItem($childKey, $this, 'json')['data'];
		$this->assertArrayHasKey('parentItem', $json);
		$childVersion = $json['version'];
		
		// But it should be removed with parentItem: false
		$json = [
			'parentItem' => false
		];
		$response = API::userPatch(
			self::$config['userID'],
			"items/$childKey",
			json_encode($json),
			["If-Unmodified-Since-Version: " . $childVersion]
		);
		$this->assert204($response);
		$json = API::getItem($childKey, $this, 'json')['data'];
		$this->assertArrayNotHasKey('parentItem', $json);
	}
Beispiel #4
0
 public function testTagDiacritics()
 {
     $data = API::createItem("book", ["tags" => [["tag" => "ëtest"]]], $this, 'jsonData');
     $version = $data['version'];
     // Add 'etest', without accent
     $data['tags'] = [["tag" => "ëtest"], ["tag" => "etest"]];
     $response = API::postItem($data);
     $this->assert200($response);
     $this->assert200ForObject($response);
     // Item version should be one greater than last update
     $data = API::getItem($data['key'], $this, 'json')['data'];
     $this->assertEquals($version + 1, $data['version']);
     $this->assertCount(2, $data['tags']);
     $this->assertContains(["tag" => "ëtest"], $data['tags']);
     $this->assertContains(["tag" => "etest"], $data['tags']);
 }
Beispiel #5
0
 public function testWebTranslationMultiple()
 {
     $title = 'Zotero: A Guide for Librarians, Researchers and Educators';
     $response = API::userPost(self::$config['userID'], "items?key=" . self::$config['apiKey'], json_encode(["url" => "http://www.amazon.com/s/field-keywords=zotero+guide+librarians"]), array("Content-Type: application/json"));
     $this->assert300($response);
     $json = json_decode($response->getBody());
     $results = get_object_vars($json->items);
     $key = array_keys($results)[0];
     $val = array_values($results)[0];
     $this->assertEquals('0', $key);
     $this->assertEquals($title, $val);
     $items = new \stdClass();
     $items->{$key} = $val;
     // Missing token
     $response = API::userPost(self::$config['userID'], "items?key=" . self::$config['apiKey'], json_encode(["url" => "http://www.amazon.com/s/field-keywords=zotero+guide+librarians", "items" => $items]), array("Content-Type: application/json"));
     $this->assert400($response, "Token not provided with selected items");
     // Invalid selection
     $items2 = clone $items;
     $invalidKey = "12345";
     $items2->{$invalidKey} = $items2->{$key};
     unset($items2->{$key});
     $response = API::userPost(self::$config['userID'], "items?key=" . self::$config['apiKey'], json_encode(["url" => "http://www.amazon.com/s/field-keywords=zotero+guide+librarians", "token" => $json->token, "items" => $items2]), array("Content-Type: application/json"));
     $this->assert400($response, "Index '{$invalidKey}' not found for URL and token");
     $response = API::userPost(self::$config['userID'], "items?key=" . self::$config['apiKey'], json_encode(["url" => "http://www.amazon.com/s/field-keywords=zotero+guide+librarians", "token" => $json->token, "items" => $items]), array("Content-Type: application/json"));
     $this->assert200($response);
     $this->assert200ForObject($response);
     $json = API::getJSONFromResponse($response);
     $itemKey = $json['success'][0];
     $data = API::getItem($itemKey, $this, 'json')['data'];
     $this->assertEquals($title, $data['title']);
 }
Beispiel #6
0
 public function testDeleteItemRelation()
 {
     $relations = array("owl:sameAs" => ["http://zotero.org/groups/1/items/AAAAAAAA", "http://zotero.org/groups/1/items/BBBBBBBB"], "dc:relation" => "http://zotero.org/users/" . self::$config['userID'] . "/items/AAAAAAAA");
     $data = API::createItem("book", array("relations" => $relations), $this, 'jsonData');
     $itemKey = $data['key'];
     // Remove a relation
     $data['relations']['owl:sameAs'] = $relations['owl:sameAs'] = $relations['owl:sameAs'][0];
     $response = API::userPut(self::$config['userID'], "items/{$itemKey}", json_encode($data));
     $this->assert204($response);
     // Make sure it's gone
     $data = API::getItem($data['key'], $this, 'json')['data'];
     $this->assertCount(sizeOf($relations), $data['relations']);
     foreach ($relations as $predicate => $object) {
         $this->assertEquals($object, $data['relations'][$predicate]);
     }
     // Delete all
     $data['relations'] = new \stdClass();
     $response = API::userPut(self::$config['userID'], "items/{$itemKey}", json_encode($data));
     $this->assert204($response);
     // Make sure they're gone
     $data = API::getItem($itemKey, $this, 'json')['data'];
     $this->assertCount(0, $data['relations']);
 }
Beispiel #7
0
 public function testCollectionItemChange()
 {
     $collectionKey1 = API::createCollection('Test', false, $this, 'key');
     $collectionKey2 = API::createCollection('Test', false, $this, 'key');
     $json = API::createItem("book", array('collections' => array($collectionKey1)), $this, 'json');
     $itemKey1 = $json['key'];
     $itemVersion1 = $json['version'];
     $this->assertEquals([$collectionKey1], $json['data']['collections']);
     $json = API::createItem("journalArticle", array('collections' => array($collectionKey2)), $this, 'json');
     $itemKey2 = $json['key'];
     $itemVersion2 = $json['version'];
     $this->assertEquals([$collectionKey2], $json['data']['collections']);
     $json = API::getCollection($collectionKey1, $this);
     $this->assertEquals(1, $json['meta']['numItems']);
     $json = API::getCollection($collectionKey2, $this);
     $this->assertEquals(1, $json['meta']['numItems']);
     $collectionData2 = $json['data'];
     $libraryVersion = API::getLibraryVersion();
     // Add items to collection
     $response = API::userPatch(self::$config['userID'], "items/{$itemKey1}", json_encode(array("collections" => array($collectionKey1, $collectionKey2))), array("Content-Type: application/json", "If-Unmodified-Since-Version: {$itemVersion1}"));
     $this->assert204($response);
     // Item version should change
     $json = API::getItem($itemKey1, $this);
     $this->assertEquals($libraryVersion + 1, $json['version']);
     // Collection timestamp shouldn't change, but numItems should
     $json = API::getCollection($collectionKey2, $this);
     $this->assertEquals(2, $json['meta']['numItems']);
     $this->assertEquals($collectionData2['version'], $json['version']);
     $collectionData2 = $json['data'];
     $libraryVersion = API::getLibraryVersion();
     // Remove collections
     $response = API::userPatch(self::$config['userID'], "items/{$itemKey2}", json_encode(array("collections" => array())), array("Content-Type: application/json", "If-Unmodified-Since-Version: {$itemVersion2}"));
     $this->assert204($response);
     // Item version should change
     $json = API::getItem($itemKey2, $this);
     $this->assertEquals($libraryVersion + 1, $json['version']);
     // Collection timestamp shouldn't change, but numItems should
     $json = API::getCollection($collectionKey2, $this);
     $this->assertEquals(1, $json['meta']['numItems']);
     $this->assertEquals($collectionData2['version'], $json['version']);
     // Check collections arrays and numItems
     $json = API::getItem($itemKey1, $this);
     $this->assertCount(2, $json['data']['collections']);
     $this->assertContains($collectionKey1, $json['data']['collections']);
     $this->assertContains($collectionKey2, $json['data']['collections']);
     $json = API::getItem($itemKey2, $this);
     $this->assertCount(0, $json['data']['collections']);
     $json = API::getCollection($collectionKey1, $this);
     $this->assertEquals(1, $json['meta']['numItems']);
     $json = API::getCollection($collectionKey2, $this);
     $this->assertEquals(1, $json['meta']['numItems']);
 }