Exemplo n.º 1
0
 public function testCreatorSummary()
 {
     $xml = API::createItem("book", array("creators" => array(array("creatorType" => "author", "name" => "Test"))), $this);
     $data = API::parseDataFromAtomEntry($xml);
     $itemKey = $data['key'];
     $json = json_decode($data['content'], true);
     $creatorSummary = (string) array_shift($xml->xpath('//atom:entry/zapi:creatorSummary'));
     $this->assertEquals("Test", $creatorSummary);
     $json['creators'][] = array("creatorType" => "author", "firstName" => "Alice", "lastName" => "Foo");
     $response = API::userPut(self::$config['userID'], "items/{$itemKey}?key=" . self::$config['apiKey'], json_encode($json));
     $this->assert204($response);
     $xml = API::getItemXML($itemKey);
     $creatorSummary = (string) array_shift($xml->xpath('//atom:entry/zapi:creatorSummary'));
     $this->assertEquals("Test and Foo", $creatorSummary);
     $data = API::parseDataFromAtomEntry($xml);
     $json = json_decode($data['content'], true);
     $json['creators'][] = array("creatorType" => "author", "firstName" => "Bob", "lastName" => "Bar");
     $response = API::userPut(self::$config['userID'], "items/{$itemKey}?key=" . self::$config['apiKey'], json_encode($json));
     $this->assert204($response);
     $xml = API::getItemXML($itemKey);
     $creatorSummary = (string) array_shift($xml->xpath('//atom:entry/zapi:creatorSummary'));
     $this->assertEquals("Test et al.", $creatorSummary);
 }
Exemplo n.º 2
0
 public function testCollectionItemUpdate()
 {
     $collectionKey = Sync::createCollection(self::$sessionID, self::$config['libraryID'], "Test", null, $this);
     $itemKey = Sync::createItem(self::$sessionID, self::$config['libraryID'], "book", null, $this);
     $xml = Sync::updated(self::$sessionID);
     $updateKey = (string) $xml['updateKey'];
     // Get the item version
     $itemXML = API::getItemXML($itemKey);
     $data = API::parseDataFromAtomEntry($itemXML);
     $json = json_decode($data['content'], true);
     $itemVersion = $json['itemVersion'];
     $this->assertNotNull($itemVersion);
     // Add via sync
     $collectionXML = $xml->updated[0]->collections[0]->collection[0];
     $collectionXML['libraryID'] = self::$config['libraryID'];
     $collectionXML->addChild("items", $itemKey);
     $data = '<data version="9"><collections>' . $collectionXML->asXML() . '</collections>' . '</data>';
     $response = Sync::upload(self::$sessionID, $updateKey, $data, true);
     Sync::waitForUpload(self::$sessionID, $response, $this);
     // Make sure item was updated
     $itemXML = API::getItemXML($itemKey);
     $data = API::parseDataFromAtomEntry($itemXML);
     $json = json_decode($data['content'], true);
     $this->assertGreaterThan($itemVersion, $json['itemVersion']);
     $itemVersion = $json['itemVersion'];
     $this->assertCount(1, $json['collections']);
     $this->assertContains($collectionKey, $json['collections']);
     // Remove via sync
     $xml = Sync::updated(self::$sessionID);
     $updateKey = (string) $xml['updateKey'];
     $collectionXML = $xml->updated[0]->collections[0]->collection[0];
     $collectionXML['libraryID'] = self::$config['libraryID'];
     unset($collectionXML->items);
     $data = '<data version="9"><collections>' . $collectionXML->asXML() . '</collections>' . '</data>';
     $response = Sync::upload(self::$sessionID, $updateKey, $data, true);
     Sync::waitForUpload(self::$sessionID, $response, $this);
     // Make sure item was removed
     $itemXML = API::getItemXML($itemKey);
     $data = API::parseDataFromAtomEntry($itemXML);
     $json = json_decode($data['content'], true);
     $this->assertGreaterThan($itemVersion, $json['itemVersion']);
     $this->assertCount(0, $json['collections']);
 }
Exemplo n.º 3
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, 'data');
     $json = json_decode($data['content'], true);
     // Remove a relation
     $json['relations']['owl:sameAs'] = $relations['owl:sameAs'] = $relations['owl:sameAs'][0];
     $response = API::userPut(self::$config['userID'], "items/{$data['key']}?key=" . self::$config['apiKey'], json_encode($json));
     $this->assert204($response);
     // Make sure it's gone
     $xml = API::getItemXML($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]);
     }
     // Delete all
     $json['relations'] = new \stdClass();
     $response = API::userPut(self::$config['userID'], "items/{$data['key']}?key=" . self::$config['apiKey'], json_encode($json));
     $this->assert204($response);
     // Make sure they're gone
     $xml = API::getItemXML($data['key']);
     $data = API::parseDataFromAtomEntry($xml);
     $json = json_decode($data['content'], true);
     $this->assertCount(0, $json['relations']);
 }
Exemplo n.º 4
0
 public function testParentItemPatch()
 {
     $xml = API::createItem("book", false, $this);
     $data = API::parseDataFromAtomEntry($xml);
     $json = json_decode($data['content'], true);
     $parentKey = $data['key'];
     $parentVersion = $data['version'];
     $xml = API::createAttachmentItem("linked_url", [], $parentKey, $this);
     $data = API::parseDataFromAtomEntry($xml);
     $json = json_decode($data['content'], true);
     $childKey = $data['key'];
     $childVersion = $data['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}?key=" . self::$config['apiKey'], json_encode($json), array("If-Unmodified-Since-Version: " . $childVersion));
     $this->assert204($response);
     $xml = API::getItemXML($childKey);
     $data = API::parseDataFromAtomEntry($xml);
     $json = json_decode($data['content'], true);
     $this->assertArrayHasKey('parentItem', $json);
 }
Exemplo n.º 5
0
 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']);
 }
Exemplo n.º 6
0
 public function testCollectionItemChange()
 {
     $collectionKey1 = API::createCollection('Test', false, $this, 'key');
     $collectionKey2 = API::createCollection('Test', false, $this, 'key');
     $xml = API::createItem("book", array('collections' => array($collectionKey1)), $this, 'atom');
     $data = API::parseDataFromAtomEntry($xml);
     $itemKey1 = $data['key'];
     $itemVersion1 = $data['version'];
     $json = json_decode($data['content']);
     $this->assertEquals(array($collectionKey1), $json->collections);
     $xml = API::createItem("journalArticle", array('collections' => array($collectionKey2)), $this, 'atom');
     $data = API::parseDataFromAtomEntry($xml);
     $itemKey2 = $data['key'];
     $itemVersion2 = $data['version'];
     $json = json_decode($data['content']);
     $this->assertEquals(array($collectionKey2), $json->collections);
     $xml = API::getCollectionXML($collectionKey1);
     $collectionData1 = API::parseDataFromAtomEntry($xml);
     $this->assertEquals(1, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems')));
     $xml = API::getCollectionXML($collectionKey2);
     $collectionData2 = API::parseDataFromAtomEntry($xml);
     $this->assertEquals(1, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems')));
     $libraryVersion = API::getLibraryVersion();
     // Add items to collection
     $response = API::userPatch(self::$config['userID'], "items/{$itemKey1}?key=" . self::$config['apiKey'], json_encode(array("collections" => array($collectionKey1, $collectionKey2))), array("Content-Type: application/json", "If-Unmodified-Since-Version: {$itemVersion1}"));
     $this->assert204($response);
     // Item version should change
     $xml = API::getItemXML($itemKey1);
     $data = API::parseDataFromAtomEntry($xml);
     $this->assertEquals($libraryVersion + 1, $data['version']);
     // Collection timestamp shouldn't change, but numItems should
     $xml = API::getCollectionXML($collectionKey2);
     $data = API::parseDataFromAtomEntry($xml);
     $this->assertEquals(2, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems')));
     $this->assertEquals($collectionData2['version'], $data['version']);
     $collectionData2 = $data;
     $libraryVersion = API::getLibraryVersion();
     // Remove collections
     $response = API::userPatch(self::$config['userID'], "items/{$itemKey2}?key=" . self::$config['apiKey'], json_encode(array("collections" => array())), array("Content-Type: application/json", "If-Unmodified-Since-Version: {$itemVersion2}"));
     $this->assert204($response);
     // Item version should change
     $xml = API::getItemXML($itemKey2);
     $data = API::parseDataFromAtomEntry($xml);
     $this->assertEquals($libraryVersion + 1, $data['version']);
     // Collection timestamp shouldn't change, but numItems should
     $xml = API::getCollectionXML($collectionKey2);
     $data = API::parseDataFromAtomEntry($xml);
     $this->assertEquals(1, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems')));
     $this->assertEquals($collectionData2['version'], $data['version']);
     // Check collections arrays and numItems
     $xml = API::getItemXML($itemKey1);
     $data = API::parseDataFromAtomEntry($xml);
     $json = json_decode($data['content']);
     $this->assertCount(2, $json->collections);
     $this->assertContains($collectionKey1, $json->collections);
     $this->assertContains($collectionKey2, $json->collections);
     $xml = API::getItemXML($itemKey2);
     $data = API::parseDataFromAtomEntry($xml);
     $json = json_decode($data['content']);
     $this->assertCount(0, $json->collections);
     $xml = API::getCollectionXML($collectionKey1);
     $this->assertEquals(1, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems')));
     $xml = API::getCollectionXML($collectionKey2);
     $this->assertEquals(1, (int) array_shift($xml->xpath('//atom:entry/zapi:numItems')));
 }
Exemplo n.º 7
0
 /**
  * When modifying a tag on an item, only the item itself should have its
  * version updated, not other items that had (and still have) the same tag
  */
 public function testTagAddItemVersionChange()
 {
     $data1 = API::createItem("book", array("tags" => array(array("tag" => "a"), array("tag" => "b"))), $this, 'data');
     $json1 = json_decode($data1['content'], true);
     $version1 = $data1['version'];
     $data2 = API::createItem("book", array("tags" => array(array("tag" => "a"), array("tag" => "c"))), $this, 'data');
     $json2 = json_decode($data2['content'], true);
     $version2 = $data2['version'];
     // Remove tag 'a' from item 1
     $json1['tags'] = array(array("tag" => "d"), array("tag" => "c"));
     $response = API::postItem($json1);
     $this->assert200($response);
     // Item 1 version should be one greater than last update
     $xml1 = API::getItemXML($json1['itemKey']);
     $data1 = API::parseDataFromAtomEntry($xml1);
     $this->assertEquals($version2 + 1, $data1['version']);
     // Item 2 version shouldn't have changed
     $xml2 = API::getItemXML($json2['itemKey']);
     $data2 = API::parseDataFromAtomEntry($xml2);
     $this->assertEquals($version2, $data2['version']);
 }
Exemplo n.º 8
0
 public function testComputerProgram()
 {
     $xml = Sync::updated(self::$sessionID);
     $updateKey = (string) $xml['updateKey'];
     $itemKey = 'AAAAAAAA';
     // Create item via sync
     $data = '<data version="9"><items><item libraryID="' . self::$config['libraryID'] . '" itemType="computerProgram" ' . 'dateAdded="2009-03-07 04:53:20" ' . 'dateModified="2009-03-07 04:54:09" ' . 'key="' . $itemKey . '">' . '<field name="version">1.0</field>' . '</item></items></data>';
     $response = Sync::upload(self::$sessionID, $updateKey, $data);
     Sync::waitForUpload(self::$sessionID, $response, $this);
     // Get item version via API
     $response = API::userGet(self::$config['userID'], "items/{$itemKey}?key=" . self::$config['apiKey'] . "&content=json");
     $this->assertEquals(200, $response->getStatus());
     $xml = API::getItemXML($itemKey);
     $data = API::parseDataFromAtomEntry($xml);
     $json = json_decode($data['content'], true);
     $this->assertEquals('1.0', $json['version']);
     $json['version'] = '1.1';
     $response = API::userPut(self::$config['userID'], "items/{$itemKey}?key=" . self::$config['apiKey'], json_encode($json));
     $this->assertEquals(204, $response->getStatus());
     $xml = Sync::updated(self::$sessionID);
     $this->assertEquals('version', (string) $xml->updated[0]->items[0]->item[0]->field[0]['name']);
 }