public function testSetItemContent() { $key = API::createItem("book", false, $this, 'key'); $attachmentKey = API::createAttachmentItem("imported_url", [], $key, $this, 'key'); $response = API::userGet(self::$config['userID'], "items/{$attachmentKey}/fulltext"); $this->assert404($response); $this->assertNull($response->getHeader("Last-Modified-Version")); $libraryVersion = API::getLibraryVersion(); $content = "Here is some full-text content"; $pages = 50; // No Content-Type $response = API::userPut(self::$config['userID'], "items/{$attachmentKey}/fulltext", $content); $this->assert400($response, "Content-Type must be application/json"); // Store content $response = API::userPut(self::$config['userID'], "items/{$attachmentKey}/fulltext", json_encode(["content" => $content, "indexedPages" => $pages, "totalPages" => $pages, "invalidParam" => "shouldBeIgnored"]), array("Content-Type: application/json")); $this->assert204($response); $contentVersion = $response->getHeader("Last-Modified-Version"); $this->assertGreaterThan($libraryVersion, $contentVersion); // Retrieve it $response = API::userGet(self::$config['userID'], "items/{$attachmentKey}/fulltext"); $this->assert200($response); $this->assertContentType("application/json", $response); $json = json_decode($response->getBody(), true); $this->assertEquals($content, $json['content']); $this->assertArrayHasKey('indexedPages', $json); $this->assertArrayHasKey('totalPages', $json); $this->assertEquals($pages, $json['indexedPages']); $this->assertEquals($pages, $json['totalPages']); $this->assertArrayNotHasKey("indexedChars", $json); $this->assertArrayNotHasKey("invalidParam", $json); $this->assertEquals($contentVersion, $response->getHeader("Last-Modified-Version")); }
public function testMultiTagDelete() { $tags1 = array("a", "aa", "b"); $tags2 = array("b", "c", "cc"); $tags3 = array("Foo"); API::createItem("book", array("tags" => array_map(function ($tag) { return array("tag" => $tag); }, $tags1)), $this, 'key'); API::createItem("book", array("tags" => array_map(function ($tag) { return array("tag" => $tag, "type" => 1); }, $tags2)), $this, 'key'); API::createItem("book", array("tags" => array_map(function ($tag) { return array("tag" => $tag); }, $tags3)), $this, 'key'); $libraryVersion = API::getLibraryVersion(); // Missing version header $response = API::userDelete(self::$config['userID'], "tags?tag=" . implode("%20||%20", array_merge($tags1, $tags2))); $this->assert428($response); // Outdated version header $response = API::userDelete(self::$config['userID'], "tags?tag=" . implode("%20||%20", array_merge($tags1, $tags2)), array("If-Unmodified-Since-Version: " . ($libraryVersion - 1))); $this->assert412($response); // Delete $response = API::userDelete(self::$config['userID'], "tags?tag=" . implode("%20||%20", array_merge($tags1, $tags2)), array("If-Unmodified-Since-Version: {$libraryVersion}")); $this->assert204($response); // Make sure they're gone $response = API::userGet(self::$config['userID'], "tags?tag=" . implode("%20||%20", array_merge($tags1, $tags2, $tags3))); $this->assert200($response); $this->assertNumResults(1, $response); }
public function testUnsupportedSettingMultiple() { $settingKey = "unsupportedSetting"; $json = array("tagColors" => array("value" => array("name" => "_READ", "color" => "#990000"), "version" => 0), $settingKey => array("value" => false, "version" => 0)); $libraryVersion = API::getLibraryVersion(); $response = API::userPut(self::$config['userID'], "settings/{$settingKey}", json_encode($json), array("Content-Type: application/json")); $this->assert400($response, "Invalid setting '{$settingKey}'"); // Valid setting shouldn't exist, and library version should be unchanged $response = API::userGet(self::$config['userID'], "settings/{$settingKey}"); $this->assert404($response); $this->assertEquals($libraryVersion, API::getLibraryVersion()); }
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']); }
public function testTagDeletePermissions() { API::userClear(self::$config['userID']); API::createItem('book', array("tags" => array(array("tag" => "A"))), $this); $libraryVersion = API::getLibraryVersion(); API::setKeyOption(self::$config['userID'], self::$config['apiKey'], 'libraryWrite', 0); $response = API::userDelete(self::$config['userID'], "tags?tag=A&key=" . self::$config['apiKey']); $this->assert403($response); API::setKeyOption(self::$config['userID'], self::$config['apiKey'], 'libraryWrite', 1); $response = API::userDelete(self::$config['userID'], "tags?tag=A&key=" . self::$config['apiKey'], array("If-Unmodified-Since-Version: {$libraryVersion}")); $this->assert204($response); }