public static function setUpBeforeClass() { parent::setUpBeforeClass(); API::userClear(self::$config['userID']); // // Collections // /*for ($i=0; $i<5; $i++) { self::$collectionKeys[] = API::createCollection("Test", false, null, 'key'); }*/ // // Items // $titles = self::$titles; $names = self::$names; for ($i = 0; $i < sizeOf(self::$titles) - 2; $i++) { $key = API::createItem("book", ["title" => array_shift($titles), "creators" => [["creatorType" => "author", "name" => array_shift($names)]]], null, 'key'); // Child attachments if (!is_null(self::$attachmentTitles[$i])) { self::$childAttachmentKeys[] = API::createAttachmentItem("imported_file", ["title" => self::$attachmentTitles[$i]], $key, null, 'key'); } // Child notes if (!is_null(self::$notes[$i])) { self::$childNoteKeys[] = API::createNoteItem(self::$notes[$i], $key, null, 'key'); } self::$itemKeys[] = $key; } // Top-level attachment self::$itemKeys[] = API::createAttachmentItem("imported_file", ["title" => array_shift($titles)], false, null, 'key'); // Top-level note self::$itemKeys[] = API::createNoteItem(array_shift($titles), false, null, 'key'); // // Searches // /*for ($i=0; $i<5; $i++) { self::$searchKeys[] = API::createSearch("Test", 'default', null, 'key'); }*/ }
public function testKeyNoteAccess() { API::userClear(self::$config['userID']); API::setKeyOption(self::$config['userID'], self::$config['apiKey'], 'libraryNotes', 1); $keys = array(); $topLevelKeys = array(); $bookKeys = array(); $xml = API::createItem('book', array("title" => "A"), $this); $data = API::parseDataFromAtomEntry($xml); $keys[] = $data['key']; $topKeys[] = $data['key']; $bookKeys[] = $data['key']; $xml = API::createNoteItem("B", false, $this); $data = API::parseDataFromAtomEntry($xml); $keys[] = $data['key']; $topKeys[] = $data['key']; $xml = API::createNoteItem("C", false, $this); $data = API::parseDataFromAtomEntry($xml); $keys[] = $data['key']; $topKeys[] = $data['key']; $xml = API::createNoteItem("D", false, $this); $data = API::parseDataFromAtomEntry($xml); $keys[] = $data['key']; $topKeys[] = $data['key']; $xml = API::createNoteItem("E", false, $this); $data = API::parseDataFromAtomEntry($xml); $keys[] = $data['key']; $topKeys[] = $data['key']; $xml = API::createItem('book', array("title" => "F"), $this); $data = API::parseDataFromAtomEntry($xml); $keys[] = $data['key']; $topKeys[] = $data['key']; $bookKeys[] = $data['key']; $xml = API::createNoteItem("G", $data['key'], $this); $data = API::parseDataFromAtomEntry($xml); $keys[] = $data['key']; // Create collection and add items to it $response = API::userPost(self::$config['userID'], "collections?key=" . self::$config['apiKey'], json_encode(array("collections" => array(array("name" => "Test", "parentCollection" => false)))), array("Content-Type: application/json")); $this->assert200ForObject($response); $collectionKey = API::getFirstSuccessKeyFromResponse($response); $response = API::userPost(self::$config['userID'], "collections/{$collectionKey}/items?key=" . self::$config['apiKey'], implode(" ", $topKeys)); $this->assert204($response); // // format=atom // // Root $response = API::userGet(self::$config['userID'], "items?key=" . self::$config['apiKey']); $this->assertNumResults(sizeOf($keys), $response); $this->assertTotalResults(sizeOf($keys), $response); // Top $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey']); $this->assertNumResults(sizeOf($topKeys), $response); $this->assertTotalResults(sizeOf($topKeys), $response); // Collection $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items/top?key=" . self::$config['apiKey']); $this->assertNumResults(sizeOf($topKeys), $response); $this->assertTotalResults(sizeOf($topKeys), $response); // // format=keys // // Root $response = API::userGet(self::$config['userID'], "items?key=" . self::$config['apiKey'] . "&format=keys"); $this->assert200($response); $this->assertCount(sizeOf($keys), explode("\n", trim($response->getBody()))); // Top $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey'] . "&format=keys"); $this->assert200($response); $this->assertCount(sizeOf($topKeys), explode("\n", trim($response->getBody()))); // Collection $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items/top?key=" . self::$config['apiKey'] . "&format=keys"); $this->assert200($response); $this->assertCount(sizeOf($topKeys), explode("\n", trim($response->getBody()))); // Remove notes privilege from key API::setKeyOption(self::$config['userID'], self::$config['apiKey'], 'libraryNotes', 0); // // format=atom // // totalResults with limit $response = API::userGet(self::$config['userID'], "items?key=" . self::$config['apiKey'] . "&limit=1"); $this->assertNumResults(1, $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // And without limit $response = API::userGet(self::$config['userID'], "items?key=" . self::$config['apiKey']); $this->assertNumResults(sizeOf($bookKeys), $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // Top $response = API::userGet(self::$config['userID'], "items/top?key=" . self::$config['apiKey']); $this->assertNumResults(sizeOf($bookKeys), $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // Collection $response = API::userGet(self::$config['userID'], "collections/{$collectionKey}/items?key=" . self::$config['apiKey']); $this->assertNumResults(sizeOf($bookKeys), $response); $this->assertTotalResults(sizeOf($bookKeys), $response); // // format=keys // $response = API::userGet(self::$config['userID'], "items?key=" . self::$config['apiKey'] . "&format=keys"); $keys = explode("\n", trim($response->getBody())); sort($keys); $this->assertEmpty(array_merge(array_diff($bookKeys, $keys), array_diff($keys, $bookKeys))); }
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 testCircularRelatedItems() { $parentKey = API::createItem("book", false, null, 'key'); $noteKeys = [API::createNoteItem("Note 1", $parentKey, null, 'key'), API::createNoteItem("Note 2", $parentKey, null, 'key'), API::createNoteItem("Note 3", $parentKey, null, 'key'), API::createNoteItem("Note 4", $parentKey, null, 'key')]; $xml = Sync::updated(self::$sessionID); $updateKey = $xml['updateKey']; $note1XML = array_shift($xml->updated[0]->items->xpath("//item[@key = '{$noteKeys[0]}']")); $note2XML = array_shift($xml->updated[0]->items->xpath("//item[@key = '{$noteKeys[1]}']")); $note3XML = array_shift($xml->updated[0]->items->xpath("//item[@key = '{$noteKeys[2]}']")); $note4XML = array_shift($xml->updated[0]->items->xpath("//item[@key = '{$noteKeys[3]}']")); $note1XML['libraryID'] = self::$config['libraryID']; $note2XML['libraryID'] = self::$config['libraryID']; $note3XML['libraryID'] = self::$config['libraryID']; $note4XML['libraryID'] = self::$config['libraryID']; $note1XML->related = implode(' ', [$parentKey, (string) $note2XML['key'], (string) $note3XML['key'], (string) $note4XML['key']]); $note2XML->related = implode(' ', [$parentKey, (string) $note1XML['key'], (string) $note3XML['key'], (string) $note4XML['key']]); $note3XML->related = implode(' ', [$parentKey, (string) $note1XML['key'], (string) $note2XML['key'], (string) $note4XML['key']]); $note4XML->related = implode(' ', [$parentKey, (string) $note1XML['key'], (string) $note2XML['key'], (string) $note3XML['key']]); $xmlstr = '<data version="9">' . '<items>' . $note1XML->asXML() . $note2XML->asXML() . $note3XML->asXML() . $note4XML->asXML() . '</items>' . '</data>'; $response = Sync::upload(self::$sessionID, $updateKey, $xmlstr); Sync::waitForUpload(self::$sessionID, $response, $this); $xml = Sync::updated(self::$sessionID); $noteXML = array_shift($xml->updated[0]->items->xpath("//item[@key = '{$noteKeys[0]}']")); $keys = split(' ', $noteXML->related); $this->assertCount(4, $keys); $this->assertContains($parentKey, $keys); $this->assertContains((string) $noteKeys[1], $keys); $this->assertContains((string) $noteKeys[2], $keys); $this->assertContains((string) $noteKeys[3], $keys); $noteXML = array_shift($xml->updated[0]->items->xpath("//item[@key = '{$noteKeys[1]}']")); $keys = split(' ', $noteXML->related); $this->assertCount(4, $keys); $this->assertContains($parentKey, $keys); $this->assertContains((string) $noteKeys[0], $keys); $this->assertContains((string) $noteKeys[2], $keys); $this->assertContains((string) $noteKeys[3], $keys); $noteXML = array_shift($xml->updated[0]->items->xpath("//item[@key = '{$noteKeys[2]}']")); $keys = split(' ', $noteXML->related); $this->assertCount(4, $keys); $this->assertContains($parentKey, $keys); $this->assertContains((string) $noteKeys[0], $keys); $this->assertContains((string) $noteKeys[1], $keys); $this->assertContains((string) $noteKeys[3], $keys); $noteXML = array_shift($xml->updated[0]->items->xpath("//item[@key = '{$noteKeys[3]}']")); $keys = split(' ', $noteXML->related); $this->assertCount(4, $keys); $this->assertContains($parentKey, $keys); $this->assertContains((string) $noteKeys[0], $keys); $this->assertContains((string) $noteKeys[1], $keys); $this->assertContains((string) $noteKeys[2], $keys); }
public function testCollectionChildItemError() { $collectionKey = API::createCollection('Test', false, $this, 'key'); $key = API::createItem("book", array(), $this, 'key'); $xml = API::createNoteItem("Test Note", $key, $this, 'atom'); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content'], true); $json['collections'] = array($collectionKey); $json['relations'] = new stdClass(); $libraryVersion = API::getLibraryVersion(); $response = API::userPut(self::$config['userID'], "items/{$data['key']}?key=" . self::$config['apiKey'], json_encode($json), ["Content-Type: application/json"]); $this->assert400($response); $this->assertEquals("Child items cannot be assigned to collections", $response->getBody()); }