Beispiel #1
0
	public function testTrash() {
		API::userClear(self::$config['userID']);
		
		$key1 = API::createItem("book", false, $this, 'key');
		$key2 = API::createItem("book", [
			"deleted" => 1
		], $this, 'key');
		
		// Item should show up in trash
		$response = API::userGet(
			self::$config['userID'],
			"items/trash"
		);
		$json = API::getJSONFromResponse($response);
		$this->assertCount(1, $json);
		$this->assertEquals($key2, $json[0]['key']);
		
		// And not show up in main items
		$response = API::userGet(
			self::$config['userID'],
			"items"
		);
		$json = API::getJSONFromResponse($response);
		$this->assertCount(1, $json);
		$this->assertEquals($key1, $json[0]['key']);
		
		// Including with ?itemKey
		$response = API::userGet(
			self::$config['userID'],
			"items?itemKey=" . $key2
		);
		$json = API::getJSONFromResponse($response);
		$this->assertCount(0, $json);
	}
Beispiel #2
0
 public function testTagsSince()
 {
     self::_testTagsSince('since');
     API::userClear(self::$config['userID']);
     self::_testTagsSince('newer');
 }
Beispiel #3
0
 public function testTagNewer()
 {
     API::userClear(self::$config['userID']);
     // Create items with tags
     API::createItem("book", array("tags" => array(array("tag" => "a"), array("tag" => "b"))), $this);
     $version = API::getLibraryVersion();
     // 'newer' shouldn't return any results
     $response = API::userGet(self::$config['userID'], "tags?newer={$version}");
     $this->assert200($response);
     $this->assertNumResults(0, $response);
     // Create another item with tags
     API::createItem("book", array("tags" => array(array("tag" => "a"), array("tag" => "c"))), $this);
     // 'newer' should return new tag (Atom)
     $response = API::userGet(self::$config['userID'], "tags?content=json&newer={$version}");
     $this->assert200($response);
     $this->assertNumResults(1, $response);
     $this->assertGreaterThan($version, $response->getHeader('Last-Modified-Version'));
     $xml = API::getXMLFromResponse($response);
     $data = API::parseDataFromAtomEntry($xml);
     $data = json_decode($data['content'], true);
     $this->assertEquals("c", $data['tag']);
     $this->assertEquals(0, $data['type']);
     // 'newer' should return new tag (JSON)
     $response = API::userGet(self::$config['userID'], "tags?newer={$version}");
     $this->assert200($response);
     $this->assertNumResults(1, $response);
     $this->assertGreaterThan($version, $response->getHeader('Last-Modified-Version'));
     $json = API::getJSONFromResponse($response)[0];
     $this->assertEquals("c", $json['tag']);
     $this->assertEquals(0, $json['meta']['type']);
 }
Beispiel #4
0
 public static function tearDownAfterClass()
 {
     parent::tearDownAfterClass();
     API::userClear(self::$config['userID']);
 }
Beispiel #5
0
 private function _testSinceContent($param)
 {
     API::userClear(self::$config['userID']);
     // Store content for one item
     $key = API::createItem("book", false, $this, 'key');
     $json = API::createAttachmentItem("imported_url", [], $key, $this, 'jsonData');
     $key1 = $json['key'];
     $content = "Here is some full-text content";
     $response = API::userPut(self::$config['userID'], "items/{$key1}/fulltext", 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');
     $json = API::createAttachmentItem("imported_url", [], $key, $this, 'jsonData');
     $key2 = $json['key'];
     $response = API::userPut(self::$config['userID'], "items/{$key2}/fulltext", 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?{$param}={$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 since=0
     $response = API::userGet(self::$config['userID'], "fulltext?{$param}=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]);
 }
Beispiel #6
0
	public function testAddFileClientV5Zip() {
		API::userClear(self::$config['userID']);
		
		// Get last storage sync
		$response = API::userGet(
			self::$config['userID'],
			"laststoragesync"
		);
		$this->assert404($response);
		
		$json = API::createItem("book", false, $this, 'jsonData');
		$key = $json['key'];
		
		$json = API::createAttachmentItem("imported_url", [], $key, $this, 'jsonData');
		$key = $json['key'];
		$version = $json['version'];
		
		$fileContents = self::getRandomUnicodeString();
		$contentType = "text/html";
		$charset = "utf-8";
		$filename = "file.html";
		$mtime = time();
		$hash = md5($fileContents);
		
		// Create ZIP file
		$zip = new \ZipArchive();
		$file = "work/$key.zip";
		if ($zip->open($file, \ZIPARCHIVE::CREATE) !== TRUE) {
			throw new Exception("Cannot open ZIP file");
		}
		$zip->addFromString($filename, $fileContents);
		$zip->addFromString("file.css", self::getRandomUnicodeString());
		$zip->close();
		$zipHash = md5_file($file);
		$zipFilename = $key . ".zip";
		$zipSize = filesize($file);
		$zipFileContents = file_get_contents($file);
		
		// Get a sync timestamp from before the file is updated
		sleep(1);
		require_once 'include/sync.inc.php';
		$sessionID = Sync::login();
		$xml = Sync::updated($sessionID);
		$lastsync = (int) $xml['timestamp'];
		Sync::logout($sessionID);
		
		//
		// Get upload authorization
		//
		$response = API::userPost(
			self::$config['userID'],
			"items/$key/file",
			$this->implodeParams([
				"md5" => $hash,
				"mtime" => $mtime,
				"filename" => $filename,
				"filesize" => $zipSize,
				"zipMD5" => $zipHash,
				"zipFilename" => $zipFilename,
				"contentType" => $contentType,
				"charset" => $charset
			]),
			[
				"Content-Type: application/x-www-form-urlencoded",
				"If-None-Match: *"
			]
		);
		$this->assert200($response);
		$json = API::getJSONFromResponse($response);
		
		self::$toDelete[] = "$zipHash";
		
		// Upload to S3
		$response = HTTP::post(
			$json['url'],
			$json['prefix'] . $zipFileContents . $json['suffix'],
			[
				"Content-Type: {$json['contentType']}"
			]
		);
		$this->assert201($response);
		
		//
		// Register upload
		//
		
		// If-Match with file hash shouldn't match unregistered file
		$response = API::userPost(
			self::$config['userID'],
			"items/$key/file",
			"upload=" . $json['uploadKey'],
			[
				"Content-Type: application/x-www-form-urlencoded",
				"If-Match: $hash"
			]
		);
		$this->assert412($response);
		
		// If-Match with ZIP hash shouldn't match unregistered file
		$response = API::userPost(
			self::$config['userID'],
			"items/$key/file",
			"upload=" . $json['uploadKey'],
			[
				"Content-Type: application/x-www-form-urlencoded",
				"If-Match: $zipHash"
			]
		);
		$this->assert412($response);
		
		$response = API::userPost(
			self::$config['userID'],
			"items/$key/file",
			"upload=" . $json['uploadKey'],
			[
				"Content-Type: application/x-www-form-urlencoded",
				"If-None-Match: *"
			]
		);
		$this->assert204($response);
		
		// Verify attachment item metadata
		$response = API::userGet(
			self::$config['userID'],
			"items/$key"
		);
		$json = API::getJSONFromResponse($response)['data'];
		
		$this->assertEquals($hash, $json['md5']);
		$this->assertEquals($mtime, $json['mtime']);
		$this->assertEquals($filename, $json['filename']);
		$this->assertEquals($contentType, $json['contentType']);
		$this->assertEquals($charset, $json['charset']);
		
		$response = API::userGet(
			self::$config['userID'],
			"laststoragesync"
		);
		$this->assert200($response);
		$this->assertRegExp('/^[0-9]{10}$/', $response->getBody());
		
		// File exists
		$response = API::userPost(
			self::$config['userID'],
			"items/$key/file",
			$this->implodeParams([
				"md5" => $hash,
				"mtime" => $mtime + 1000,
				"filename" => $filename,
				"filesize" => $zipSize,
				"zip" => 1,
				"zipMD5" => $zipHash,
				"zipFilename" => $zipFilename
			]),
			[
				"Content-Type: application/x-www-form-urlencoded",
				"If-Match: $hash"
			]
		);
		$this->assert200($response);
		$json = API::getJSONFromResponse($response);
		$this->assertArrayHasKey("exists", $json);
		
		// Get attachment via classic sync
		$sessionID = Sync::login();
		$xml = Sync::updated($sessionID, 2);
		$this->assertEquals(1, $xml->updated[0]->items->count());
		$itemXML = $xml->xpath("//updated/items/item[@key='$key']")[0];
		$this->assertEquals($contentType, (string) $itemXML['mimeType']);
		$this->assertEquals($charset, (string) $itemXML['charset']);
		$this->assertEquals($hash, (string) $itemXML['storageHash']);
		$this->assertEquals($mtime + 1000, (string) $itemXML['storageModTime']);
		Sync::logout($sessionID);
	}
Beispiel #7
0
 private function _testPagination($objectType)
 {
     API::userClear(self::$config['userID']);
     $objectTypePlural = API::getPluralObjectType($objectType);
     $limit = 2;
     $totalResults = 5;
     $formats = ['json', 'atom', 'keys'];
     switch ($objectType) {
         case 'collection':
             for ($i = 0; $i < $totalResults; $i++) {
                 API::createCollection("Test", false, $this, 'key');
             }
             break;
         case 'item':
             //
             // Items
             //
             for ($i = 0; $i < $totalResults; $i++) {
                 API::createItem("book", false, $this, 'key');
             }
             break;
             //
             // Searches
             //
         //
         // Searches
         //
         case 'search':
             for ($i = 0; $i < $totalResults; $i++) {
                 API::createSearch("Test", 'default', $this, 'key');
             }
             break;
         case 'tag':
             API::createItem("book", ['tags' => ['a', 'b']], $this);
             API::createItem("book", ['tags' => ['c', 'd', 'e']], $this);
             $formats = array_filter($formats, function ($val) {
                 return !in_array($val, ['keys']);
             });
             break;
         case 'group':
             // Change if the config changes
             $limit = 1;
             $totalResults = self::$config['numOwnedGroups'];
             $formats = array_filter($formats, function ($val) {
                 return !in_array($val, ['keys']);
             });
             break;
     }
     foreach ($formats as $format) {
         $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?limit={$limit}&format={$format}");
         $this->assert200($response);
         $this->assertNumResults($limit, $response);
         $this->assertTotalResults($totalResults, $response);
         $links = $this->parseLinkHeader($response->getHeader('Link'));
         $this->assertArrayNotHasKey('first', $links);
         $this->assertArrayNotHasKey('prev', $links);
         $this->assertArrayHasKey('next', $links);
         $this->assertEquals($limit, $links['next']['params']['start']);
         $this->assertEquals($limit, $links['next']['params']['limit']);
         $this->assertArrayHasKey('last', $links);
         $lastStart = $totalResults - $totalResults % $limit;
         if ($lastStart == $totalResults) {
             $lastStart -= $limit;
         }
         $this->assertEquals($lastStart, $links['last']['params']['start']);
         $this->assertEquals($limit, $links['last']['params']['limit']);
         // TODO: Test with more groups
         if ($objectType == 'group') {
             continue;
         }
         // Start at 1
         $start = 1;
         $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?start={$start}&limit={$limit}&format={$format}");
         $this->assert200($response);
         $this->assertNumResults($limit, $response);
         $this->assertTotalResults($totalResults, $response);
         $links = $this->parseLinkHeader($response->getHeader('Link'));
         $this->assertArrayHasKey('first', $links);
         $this->assertArrayNotHasKey('start', $links['first']['params']);
         $this->assertEquals($limit, $links['first']['params']['limit']);
         $this->assertArrayHasKey('prev', $links);
         $this->assertArrayNotHasKey('start', $links['prev']['params']);
         $this->assertEquals($limit, $links['prev']['params']['limit']);
         $this->assertArrayHasKey('next', $links);
         $this->assertEquals($start + $limit, $links['next']['params']['start']);
         $this->assertEquals($limit, $links['next']['params']['limit']);
         $this->assertArrayHasKey('last', $links);
         $lastStart = $totalResults - $totalResults % $limit;
         if ($lastStart == $totalResults) {
             $lastStart -= $limit;
         }
         $this->assertEquals($lastStart, $links['last']['params']['start']);
         $this->assertEquals($limit, $links['last']['params']['limit']);
         // Start at 2
         $start = 2;
         $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?start={$start}&limit={$limit}&format={$format}");
         $this->assert200($response);
         $this->assertNumResults($limit, $response);
         $this->assertTotalResults($totalResults, $response);
         $links = $this->parseLinkHeader($response->getHeader('Link'));
         $this->assertArrayHasKey('first', $links);
         $this->assertArrayNotHasKey('start', $links['first']['params']);
         $this->assertEquals($limit, $links['first']['params']['limit']);
         $this->assertArrayHasKey('prev', $links);
         $this->assertArrayNotHasKey('start', $links['prev']['params']);
         $this->assertEquals($limit, $links['prev']['params']['limit']);
         $this->assertArrayHasKey('next', $links);
         $this->assertEquals($start + $limit, $links['next']['params']['start']);
         $this->assertEquals($limit, $links['next']['params']['limit']);
         $this->assertArrayHasKey('last', $links);
         $lastStart = $totalResults - $totalResults % $limit;
         if ($lastStart == $totalResults) {
             $lastStart -= $limit;
         }
         $this->assertEquals($lastStart, $links['last']['params']['start']);
         $this->assertEquals($limit, $links['last']['params']['limit']);
         // Start at 3
         $start = 3;
         $response = API::userGet(self::$config['userID'], "{$objectTypePlural}?start={$start}&limit={$limit}&format={$format}");
         $this->assert200($response);
         $this->assertNumResults($limit, $response);
         $this->assertTotalResults($totalResults, $response);
         $links = $this->parseLinkHeader($response->getHeader('Link'));
         $this->assertArrayHasKey('first', $links);
         $this->assertArrayNotHasKey('start', $links['first']['params']);
         $this->assertEquals($limit, $links['first']['params']['limit']);
         $this->assertArrayHasKey('prev', $links);
         $this->assertEquals(max(0, $start - $limit), $links['prev']['params']['start']);
         $this->assertEquals($limit, $links['prev']['params']['limit']);
         $this->assertArrayNotHasKey('next', $links);
         $this->assertArrayHasKey('last', $links);
         $lastStart = $totalResults - $totalResults % $limit;
         if ($lastStart == $totalResults) {
             $lastStart -= $limit;
         }
         $this->assertEquals($lastStart, $links['last']['params']['start']);
         $this->assertEquals($limit, $links['last']['params']['limit']);
     }
 }
Beispiel #8
0
 public static function tearDownAfterClass()
 {
     parent::tearDownAfterClass();
     require 'include/config.inc.php';
     API::userClear($config['userID']);
 }
Beispiel #9
0
 public function testAddFileClientZip()
 {
     API::userClear(self::$config['userID']);
     $auth = array('username' => self::$config['username'], 'password' => self::$config['password']);
     // Get last storage sync
     $response = API::userGet(self::$config['userID'], "laststoragesync?auth=1", array(), $auth);
     $this->assert404($response);
     $json = API::createItem("book", false, $this, 'jsonData');
     $key = $json['key'];
     $fileContentType = "text/html";
     $fileCharset = "UTF-8";
     $fileFilename = "file.html";
     $fileModtime = time();
     $json = API::createAttachmentItem("imported_url", [], $key, $this, 'jsonData');
     $key = $json['key'];
     $version = $json['version'];
     $json['contentType'] = $fileContentType;
     $json['charset'] = $fileCharset;
     $json['filename'] = $fileFilename;
     $response = API::userPut(self::$config['userID'], "items/{$key}", json_encode($json), array("Content-Type: application/json"));
     $this->assert204($response);
     // Get a sync timestamp from before the file is updated
     sleep(1);
     require_once 'include/sync.inc.php';
     $sessionID = Sync::login();
     $xml = Sync::updated($sessionID);
     $lastsync = (int) $xml['timestamp'];
     Sync::logout($sessionID);
     // Get file info
     $response = API::userGet(self::$config['userID'], "items/{$json['key']}/file?auth=1&iskey=1&version=1&info=1", array(), $auth);
     $this->assert404($response);
     $zip = new \ZipArchive();
     $file = "work/{$key}.zip";
     if ($zip->open($file, \ZIPARCHIVE::CREATE) !== TRUE) {
         throw new Exception("Cannot open ZIP file");
     }
     $zip->addFromString($fileFilename, self::getRandomUnicodeString());
     $zip->addFromString("file.css", self::getRandomUnicodeString());
     $zip->close();
     $hash = md5_file($file);
     $filename = $key . ".zip";
     $size = filesize($file);
     $fileContents = file_get_contents($file);
     // Get upload authorization
     $response = API::userPost(self::$config['userID'], "items/{$json['key']}/file?auth=1&iskey=1&version=1", $this->implodeParams(array("md5" => $hash, "filename" => $filename, "filesize" => $size, "mtime" => $fileModtime, "zip" => 1)), array("Content-Type: application/x-www-form-urlencoded"), $auth);
     $this->assert200($response);
     $this->assertContentType("application/xml", $response);
     $xml = new SimpleXMLElement($response->getBody());
     self::$toDelete[] = "{$hash}";
     $boundary = "---------------------------" . rand();
     $postData = "";
     foreach ($xml->params->children() as $key => $val) {
         $postData .= "--" . $boundary . "\r\nContent-Disposition: form-data; " . "name=\"{$key}\"\r\n\r\n{$val}\r\n";
     }
     $postData .= "--" . $boundary . "\r\nContent-Disposition: form-data; " . "name=\"file\"\r\n\r\n" . $fileContents . "\r\n";
     $postData .= "--" . $boundary . "--";
     // Upload to S3
     $response = HTTP::post((string) $xml->url, $postData, array("Content-Type: multipart/form-data; boundary=" . $boundary));
     $this->assert201($response);
     //
     // Register upload
     //
     $response = API::userPost(self::$config['userID'], "items/{$json['key']}/file?auth=1&iskey=1&version=1", "update=" . $xml->key . "&mtime=" . $fileModtime, array("Content-Type: application/x-www-form-urlencoded"), $auth);
     $this->assert204($response);
     // Verify attachment item metadata
     $response = API::userGet(self::$config['userID'], "items/{$json['key']}");
     $json = API::getJSONFromResponse($response)['data'];
     $this->assertEquals($hash, $json['md5']);
     $this->assertEquals($fileFilename, $json['filename']);
     $this->assertEquals($fileModtime, $json['mtime']);
     // Make sure attachment item wasn't updated (or else the client
     // will get a conflict when it tries to update the metadata)
     $sessionID = Sync::login();
     $xml = Sync::updated($sessionID, $lastsync);
     Sync::logout($sessionID);
     $this->assertEquals(0, $xml->updated[0]->count());
     $response = API::userGet(self::$config['userID'], "laststoragesync?auth=1", array(), array('username' => self::$config['username'], 'password' => self::$config['password']));
     $this->assert200($response);
     $mtime = $response->getBody();
     $this->assertRegExp('/^[0-9]{10}$/', $mtime);
     // File exists
     $response = API::userPost(self::$config['userID'], "items/{$json['key']}/file?auth=1&iskey=1&version=1", $this->implodeParams(array("md5" => $hash, "filename" => $filename, "filesize" => $size, "mtime" => $fileModtime + 1000, "zip" => 1)), array("Content-Type: application/x-www-form-urlencoded"), $auth);
     $this->assert200($response);
     $this->assertContentType("application/xml", $response);
     $this->assertEquals("<exists/>", $response->getBody());
     // Make sure attachment item still wasn't updated
     $sessionID = Sync::login();
     $xml = Sync::updated($sessionID, $lastsync);
     Sync::logout($sessionID);
     $this->assertEquals(0, $xml->updated[0]->count());
 }
Beispiel #10
0
 private function _testPartialWriteFailureWithUnchanged($objectType)
 {
     API::userClear(self::$config['userID']);
     $objectTypePlural = API::getPluralObjectType($objectType);
     switch ($objectType) {
         case 'collection':
             $json1 = API::createCollection("Test", false, $this, 'jsonData');
             $json2 = array("name" => str_repeat("1234567890", 6554));
             $json3 = array("name" => "Test");
             break;
         case 'item':
             $json1 = API::createItem("book", array("title" => "Title"), $this, 'jsonData');
             $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'));
             $json1 = API::createSearch("Name", $conditions, $this, 'jsonData');
             $json2 = array("name" => str_repeat("1234567890", 6554), "conditions" => $conditions);
             $json3 = array("name" => "Test", "conditions" => $conditions);
             break;
     }
     $response = API::userPost(self::$config['userID'], "{$objectTypePlural}", json_encode([$json1, $json2, $json3]), array("Content-Type: application/json"));
     $this->assert200($response);
     $json = API::getJSONFromResponse($response);
     $this->assertUnchangedForObject($response, 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);
     }
 }
Beispiel #11
0
 public function tearDown()
 {
     API::userClear(self::$config['userID']);
     API::groupClear(self::$config['ownedPrivateGroupID']);
 }
Beispiel #12
0
 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);
 }
Beispiel #13
0
 public function testSortDirection()
 {
     API::userClear(self::$config['userID']);
     // Setup
     $dataArray = [];
     $dataArray[] = API::createItem("book", ['title' => "B", 'creators' => [["creatorType" => "author", "name" => "B"]], 'dateAdded' => '2014-02-05T00:00:00Z', 'dateModified' => '2014-04-05T01:00:00Z'], $this, 'jsonData');
     $dataArray[] = API::createItem("journalArticle", ['title' => "A", 'creators' => [["creatorType" => "author", "name" => "A"]], 'dateAdded' => '2014-02-04T00:00:00Z', 'dateModified' => '2014-01-04T01:00:00Z'], $this, 'jsonData');
     $dataArray[] = API::createItem("newspaperArticle", ['title' => "F", 'creators' => [["creatorType" => "author", "name" => "F"]], 'dateAdded' => '2014-02-03T00:00:00Z', 'dateModified' => '2014-02-03T01:00:00Z'], $this, 'jsonData');
     $dataArray[] = API::createItem("book", ['title' => "C", 'creators' => [["creatorType" => "author", "name" => "C"]], 'dateAdded' => '2014-02-02T00:00:00Z', 'dateModified' => '2014-03-02T01:00:00Z'], $this, 'jsonData');
     // Get sorted keys
     usort($dataArray, function ($a, $b) {
         return strcmp($a['dateAdded'], $b['dateAdded']);
     });
     $keysByDateAddedAscending = array_map(function ($data) {
         return $data['key'];
     }, $dataArray);
     $keysByDateAddedDescending = array_reverse($keysByDateAddedAscending);
     // Ascending
     $response = API::userGet(self::$config['userID'], "items?format=keys&sort=dateAdded&direction=asc");
     $this->assert200($response);
     $this->assertEquals($keysByDateAddedAscending, explode("\n", trim($response->getBody())));
     $response = API::userGet(self::$config['userID'], "items?format=json&sort=dateAdded&direction=asc");
     $this->assert200($response);
     $json = API::getJSONFromResponse($response);
     $keys = array_map(function ($val) {
         return $val['key'];
     }, $json);
     $this->assertEquals($keysByDateAddedAscending, $keys);
     $response = API::userGet(self::$config['userID'], "items?format=atom&sort=dateAdded&direction=asc");
     $this->assert200($response);
     $xml = API::getXMLFromResponse($response);
     $keys = array_map(function ($val) {
         return (string) $val;
     }, $xml->xpath('//atom:entry/zapi:key'));
     $this->assertEquals($keysByDateAddedAscending, $keys);
     // Ascending using old 'order'/'sort' instead of 'sort'/'direction'
     $response = API::userGet(self::$config['userID'], "items?format=keys&order=dateAdded&sort=asc");
     $this->assert200($response);
     $this->assertEquals($keysByDateAddedAscending, explode("\n", trim($response->getBody())));
     $response = API::userGet(self::$config['userID'], "items?format=json&order=dateAdded&sort=asc");
     $this->assert200($response);
     $json = API::getJSONFromResponse($response);
     $keys = array_map(function ($val) {
         return $val['key'];
     }, $json);
     $this->assertEquals($keysByDateAddedAscending, $keys);
     $response = API::userGet(self::$config['userID'], "items?format=atom&order=dateAdded&sort=asc");
     $this->assert200($response);
     $xml = API::getXMLFromResponse($response);
     $keys = array_map(function ($val) {
         return (string) $val;
     }, $xml->xpath('//atom:entry/zapi:key'));
     $this->assertEquals($keysByDateAddedAscending, $keys);
     // Deprecated 'order'/'sort', but the wrong way
     $response = API::userGet(self::$config['userID'], "items?format=keys&sort=dateAdded&order=asc");
     $this->assert200($response);
     $this->assertEquals($keysByDateAddedAscending, explode("\n", trim($response->getBody())));
     // Descending
     $response = API::userGet(self::$config['userID'], "items?format=keys&sort=dateAdded&direction=desc");
     $this->assert200($response);
     $this->assertEquals($keysByDateAddedDescending, explode("\n", trim($response->getBody())));
     $response = API::userGet(self::$config['userID'], "items?format=json&sort=dateAdded&direction=desc");
     $this->assert200($response);
     $json = API::getJSONFromResponse($response);
     $keys = array_map(function ($val) {
         return $val['key'];
     }, $json);
     $this->assertEquals($keysByDateAddedDescending, $keys);
     $response = API::userGet(self::$config['userID'], "items?format=atom&sort=dateAdded&direction=desc");
     $this->assert200($response);
     $xml = API::getXMLFromResponse($response);
     $keys = array_map(function ($val) {
         return (string) $val;
     }, $xml->xpath('//atom:entry/zapi:key'));
     $this->assertEquals($keysByDateAddedDescending, $keys);
     // Descending
     $response = API::userGet(self::$config['userID'], "items?format=keys&order=dateAdded&sort=desc");
     $this->assert200($response);
     $this->assertEquals($keysByDateAddedDescending, explode("\n", trim($response->getBody())));
     $response = API::userGet(self::$config['userID'], "items?format=json&order=dateAdded&sort=desc");
     $this->assert200($response);
     $json = API::getJSONFromResponse($response);
     $keys = array_map(function ($val) {
         return $val['key'];
     }, $json);
     $this->assertEquals($keysByDateAddedDescending, $keys);
     $response = API::userGet(self::$config['userID'], "items?format=atom&order=dateAdded&sort=desc");
     $this->assert200($response);
     $xml = API::getXMLFromResponse($response);
     $keys = array_map(function ($val) {
         return (string) $val;
     }, $xml->xpath('//atom:entry/zapi:key'));
     $this->assertEquals($keysByDateAddedDescending, $keys);
 }