Exemplo n.º 1
0
 public function testNewEmptyBookItemMultiple()
 {
     $json = API::getItemTemplate("book");
     $data = array();
     $json->title = "A";
     $data[] = $json;
     $json2 = clone $json;
     $json2->title = "B";
     $data[] = $json2;
     $json3 = clone $json;
     $json3->title = "C";
     $data[] = $json3;
     $response = API::postItems($data);
     $this->assert200($response);
     $json = API::getJSONFromResponse($response);
     $xml = API::getItemXML($json['success'], $this);
     $contents = $xml->xpath('/atom:feed/atom:entry/atom:content');
     $content = json_decode(array_shift($contents));
     $this->assertEquals("A", $content->title);
     $content = json_decode(array_shift($contents));
     $this->assertEquals("B", $content->title);
     $content = json_decode(array_shift($contents));
     $this->assertEquals("C", $content->title);
 }
Exemplo n.º 2
0
 public function testRelatedItemRelationsSingleRequest()
 {
     $uriPrefix = "http://zotero.org/users/" . self::$config['userID'] . "/items/";
     // TEMP: Use autoloader
     require_once '../../model/Utilities.inc.php';
     require_once '../../model/ID.inc.php';
     $item1Key = \Zotero_ID::getKey();
     $item2Key = \Zotero_ID::getKey();
     $item1URI = $uriPrefix . $item1Key;
     $item2URI = $uriPrefix . $item2Key;
     $item1JSON = API::getItemTemplate('book');
     $item1JSON->itemKey = $item1Key;
     $item1JSON->itemVersion = 0;
     $item1JSON->relations->{'dc:relation'} = $item2URI;
     $item2JSON = API::getItemTemplate('book');
     $item2JSON->itemKey = $item2Key;
     $item2JSON->itemVersion = 0;
     $response = API::postItems([$item1JSON, $item2JSON]);
     $this->assert200($response);
     $json = API::getJSONFromResponse($response);
     // Make sure it exists on item 1
     $xml = API::getItemXML($item1JSON->itemKey);
     $data = API::parseDataFromAtomEntry($xml);
     $json = json_decode($data['content'], true);
     $this->assertCount(1, $json['relations']);
     $this->assertEquals($item2URI, $json['relations']['dc:relation']);
     // And item 2, since related items are bidirectional
     $xml = API::getItemXML($item2JSON->itemKey);
     $data = API::parseDataFromAtomEntry($xml);
     $json = json_decode($data['content'], true);
     $this->assertCount(1, $json['relations']);
     $this->assertEquals($item1URI, $json['relations']['dc:relation']);
 }
Exemplo n.º 3
0
 public function testSyncUploadUnchanged()
 {
     $data1 = API::createItem("audioRecording", array("title" => "Test", "relations" => array('owl:sameAs' => 'http://zotero.org/groups/1/items/AAAAAAAA')), null, 'data');
     // dc:relation already exists, so item shouldn't change
     $data2 = API::createItem("interview", array("relations" => array('dc:relation' => 'http://zotero.org/users/' . self::$config['userID'] . '/items/' . $data1['key'])), null, 'data');
     // Upload unchanged via sync
     $xml = Sync::updated(self::$sessionID);
     $updateKey = $xml['updateKey'];
     $lastSyncTimestamp = $xml['timestamp'];
     $itemXML1 = array_shift($xml->updated[0]->items[0]->xpath("item[@key='{$data1['key']}']"));
     $itemXML2 = array_shift($xml->updated[0]->items[0]->xpath("item[@key='{$data2['key']}']"));
     $itemXML1['libraryID'] = self::$config['libraryID'];
     $itemXML2['libraryID'] = self::$config['libraryID'];
     $xmlstr = '<data version="9">' . '<items>' . $itemXML1->asXML() . $itemXML2->asXML() . '</items>' . '</data>';
     $response = Sync::upload(self::$sessionID, $updateKey, $xmlstr);
     Sync::waitForUpload(self::$sessionID, $response, $this);
     // Check via API to make sure they're the same
     $response = API::userGet(self::$config['userID'], "items?key=" . self::$config['apiKey'] . "&format=versions");
     $json = API::getJSONFromResponse($response);
     $this->assertEquals($data1['version'], $json[$data1['key']]);
     $this->assertEquals($data2['version'], $json[$data2['key']]);
 }
Exemplo n.º 4
0
 public function testEditMultipleCollections()
 {
     $xml = API::createCollection("Test 1", false, $this, 'atom');
     $data = API::parseDataFromAtomEntry($xml);
     $key1 = $data['key'];
     $xml = API::createCollection("Test 2", false, $this, 'atom');
     $data = API::parseDataFromAtomEntry($xml);
     $key2 = $data['key'];
     $newName1 = "Test 1 Modified";
     $newName2 = "Test 2 Modified";
     $response = API::userPost(self::$config['userID'], "collections?key=" . self::$config['apiKey'], json_encode(array("collections" => array(array('collectionKey' => $key1, 'name' => $newName1), array('collectionKey' => $key2, 'name' => $newName2)))), array("Content-Type: application/json", "If-Unmodified-Since-Version: " . $data['version']));
     $this->assert200($response);
     $json = API::getJSONFromResponse($response);
     $this->assertCount(2, $json['success']);
     $xml = API::getCollectionXML($json['success']);
     $this->assertEquals(2, (int) array_shift($xml->xpath('/atom:feed/zapi:totalResults')));
     $contents = $xml->xpath('/atom:feed/atom:entry/atom:content');
     $content = json_decode(array_shift($contents));
     $this->assertEquals($newName1, $content->name);
     $this->assertFalse($content->parentCollection);
     $content = json_decode(array_shift($contents));
     $this->assertEquals($newName2, $content->name);
     $this->assertFalse($content->parentCollection);
 }
Exemplo n.º 5
0
 public function testNewerContent()
 {
     API::userClear(self::$config['userID']);
     // Store content for one item
     $key = API::createItem("book", false, $this, 'key');
     $xml = API::createAttachmentItem("imported_url", [], $key, $this, 'atom');
     $data = API::parseDataFromAtomEntry($xml);
     $key1 = $data['key'];
     $content = "Here is some full-text content";
     $response = API::userPut(self::$config['userID'], "items/{$key1}/fulltext?key=" . self::$config['apiKey'], json_encode(["content" => $content]), array("Content-Type: application/json"));
     $this->assert204($response);
     $contentVersion1 = $response->getHeader("Last-Modified-Version");
     $this->assertGreaterThan(0, $contentVersion1);
     // And another
     $key = API::createItem("book", false, $this, 'key');
     $xml = API::createAttachmentItem("imported_url", [], $key, $this, 'atom');
     $data = API::parseDataFromAtomEntry($xml);
     $key2 = $data['key'];
     $response = API::userPut(self::$config['userID'], "items/{$key2}/fulltext?key=" . self::$config['apiKey'], json_encode(["content" => $content]), array("Content-Type: application/json"));
     $this->assert204($response);
     $contentVersion2 = $response->getHeader("Last-Modified-Version");
     $this->assertGreaterThan(0, $contentVersion2);
     // Get newer one
     $response = API::userGet(self::$config['userID'], "fulltext?key=" . self::$config['apiKey'] . "&newer={$contentVersion1}");
     $this->assert200($response);
     $this->assertContentType("application/json", $response);
     $this->assertEquals($contentVersion2, $response->getHeader("Last-Modified-Version"));
     $json = API::getJSONFromResponse($response);
     $this->assertCount(1, $json);
     $this->assertArrayHasKey($key2, $json);
     $this->assertEquals($contentVersion2, $json[$key2]);
     // Get both with newer=0
     $response = API::userGet(self::$config['userID'], "fulltext?key=" . self::$config['apiKey'] . "&newer=0");
     $this->assert200($response);
     $this->assertContentType("application/json", $response);
     $json = API::getJSONFromResponse($response);
     $this->assertCount(2, $json);
     $this->assertArrayHasKey($key1, $json);
     $this->assertEquals($contentVersion1, $json[$key1]);
     $this->assertArrayHasKey($key1, $json);
     $this->assertEquals($contentVersion2, $json[$key2]);
 }
Exemplo n.º 6
0
 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);
     }
 }