private function _testAPINewerTimestamp($objectType) { $objectTypePlural = API::getPluralObjectType($objectType); $xml = Sync::updated(self::$sessionID); $lastSyncTimestamp = $xml['timestamp']; // Create via sync switch ($objectType) { case 'collection': $keys[] = Sync::createCollection(self::$sessionID, self::$config['libraryID'], "Test", false, $this); break; case 'item': $keys[] = Sync::createItem(self::$sessionID, self::$config['libraryID'], "book", false, $this); break; case 'search': $keys[] = Sync::createSearch(self::$sessionID, self::$config['libraryID'], "Test", 'default', $this); break; } $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'] . "&newertime={$lastSyncTimestamp}&format=keys"); $this->assertEquals(200, $response->getStatus()); $responseKeys = explode("\n", trim($response->getBody())); $this->assertCount(sizeOf($keys), $responseKeys); foreach ($keys as $key) { $this->assertContains($key, $responseKeys); } // Should be empty with later timestamp $xml = Sync::updated(self::$sessionID); $lastSyncTimestamp = $xml['timestamp']; $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?key=" . self::$config['apiKey'] . "&newertime=" . ($lastSyncTimestamp + 2) . "&format=keys"); $this->assertEquals(200, $response->getStatus()); $this->assertEquals("", trim($response->getBody())); }
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']); }
public function testCachedItem() { $itemKey = Sync::createItem(self::$sessionID, self::$config['libraryID'], "book", array("title" => "Test", "numPages" => "204"), $this); Sync::updated(self::$sessionID); $xml = API::getItemXML($itemKey); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content'], true); $json['creators'] = array(array("firstName" => "First", "lastName" => "Last", "creatorType" => "author")); $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("Test", $xml->updated[0]->items[0]->item[0]->field[0]); $this->assertEquals("204", $xml->updated[0]->items[0]->item[0]->field[1]); $this->assertEquals(1, $xml->updated[0]->items[0]->item[0]->creator->count()); // Fully cached response $xml = Sync::updated(self::$sessionID); $this->assertEquals("Test", $xml->updated[0]->items[0]->item[0]->field[0]); $this->assertEquals("204", $xml->updated[0]->items[0]->item[0]->field[1]); $this->assertEquals(1, $xml->updated[0]->items[0]->item[0]->creator->count()); // Item-level caching $xml = Sync::updated(self::$sessionID, 2); $this->assertEquals("Test", $xml->updated[0]->items[0]->item[0]->field[0]); $this->assertEquals("204", $xml->updated[0]->items[0]->item[0]->field[1]); $this->assertEquals(1, $xml->updated[0]->items[0]->item[0]->creator->count()); $xml = API::getItemXML($itemKey); $data = API::parseDataFromAtomEntry($xml); $json = json_decode($data['content'], true); $json['title'] = "Test 2"; $json['creators'] = array(array("firstName" => "First", "lastName" => "Last", "creatorType" => "author"), array("name" => "Test Name", "creatorType" => "editor")); $response = API::userPut(self::$config['userID'], "items/{$itemKey}?key=" . self::$config['apiKey'], json_encode($json)); $xml = Sync::updated(self::$sessionID); $this->assertEquals("Test 2", $xml->updated[0]->items[0]->item[0]->field[0]); $this->assertEquals("204", $xml->updated[0]->items[0]->item[0]->field[1]); $this->assertEquals(2, $xml->updated[0]->items[0]->item[0]->creator->count()); $xml = Sync::updated(self::$sessionID, 3); $this->assertEquals("Test 2", $xml->updated[0]->items[0]->item[0]->field[0]); $this->assertEquals("204", $xml->updated[0]->items[0]->item[0]->field[1]); $this->assertEquals(2, $xml->updated[0]->items[0]->item[0]->creator->count()); }
private function _testDeleteAndDeleted($objectType) { API::userClear(self::$config['userID']); $objectTypePlural = API::getPluralObjectType($objectType); $xml = Sync::updated(self::$sessionID); $lastSyncTimestamp = (int) $xml['timestamp']; // Create via sync switch ($objectType) { case 'item': $keys[] = Sync::createItem(self::$sessionID, self::$config['libraryID'], "book", false, $this); break; case 'setting': $settingKey = "tagColors"; $response = API::userPut(self::$config['userID'], "settings/{$settingKey}?key=" . self::$config['apiKey'], json_encode(array("value" => array(array("name" => "_READ", "color" => "#990000")))), array("Content-Type: application/json", "If-Unmodified-Since-Version: 0")); $this->assertEquals(204, $response->getStatus()); $keys[] = $settingKey; break; } // Check via API foreach ($keys as $key) { $response = API::userGet(self::$config['userID'], "{$objectTypePlural}/{$key}?key=" . self::$config['apiKey']); $this->assertEquals(200, $response->getStatus()); $version = $response->getHeader("Last-Modified-Version"); $this->assertNotNull($version); } // Get empty deleted via API $response = API::userGet(self::$config['userID'], "deleted?key=" . self::$config['apiKey'] . "&newer={$version}"); $this->assertEquals(200, $response->getStatus()); $json = json_decode($response->getBody(), true); $this->assertEmpty($json[$objectTypePlural]); // Get empty deleted via API with newertime $response = API::userGet(self::$config['userID'], "deleted?key=" . self::$config['apiKey'] . "&newertime={$lastSyncTimestamp}"); $this->assertEquals(200, $response->getStatus()); $json = json_decode($response->getBody(), true); $this->assertEmpty($json[$objectTypePlural]); // Delete via sync foreach ($keys as $key) { switch ($objectType) { case 'item': Sync::deleteItem(self::$sessionID, self::$config['libraryID'], $key, $this); break; case 'setting': // Delete via sync $xml = Sync::updated(self::$sessionID); $updateKey = (string) $xml['updateKey']; $xmlstr = '<data version="9">' . '<deleted>' . '<settings>' . '<setting libraryID="' . self::$config['libraryID'] . '" key="' . $key . '"/>' . '</settings>' . '</deleted>' . '</data>'; $response = Sync::upload(self::$sessionID, $updateKey, $xmlstr); Sync::waitForUpload(self::$sessionID, $response, $this); break; } } // Check 404 via API foreach ($keys as $key) { $response = API::userGet(self::$config['userID'], "{$objectTypePlural}/{$key}?key=" . self::$config['apiKey']); $this->assertEquals(404, $response->getStatus()); } // Get deleted via API $response = API::userGet(self::$config['userID'], "deleted?key=" . self::$config['apiKey'] . "&newer={$version}"); $this->assertEquals(200, $response->getStatus()); $json = json_decode($response->getBody(), true); $this->assertArrayHasKey($objectTypePlural, $json); $this->assertCount(sizeOf($keys), $json[$objectTypePlural]); foreach ($keys as $key) { $this->assertContains($key, $json[$objectTypePlural]); } // Get deleted via API with newertime $response = API::userGet(self::$config['userID'], "deleted?key=" . self::$config['apiKey'] . "&newertime={$lastSyncTimestamp}"); $this->assertEquals(200, $response->getStatus()); $json = json_decode($response->getBody(), true); $this->assertArrayHasKey($objectTypePlural, $json); $this->assertCount(sizeOf($keys), $json[$objectTypePlural]); foreach ($keys as $key) { $this->assertContains($key, $json[$objectTypePlural]); } // Should be empty with later newertime $xml = Sync::updated(self::$sessionID); $lastSyncTimestamp = (int) $xml['timestamp']; $response = API::userGet(self::$config['userID'], "deleted?key=" . self::$config['apiKey'] . "&newertime=" . ($lastSyncTimestamp + 2)); $this->assertEquals(200, $response->getStatus()); $json = json_decode($response->getBody(), true); $this->assertEmpty($json[$objectTypePlural]); }