Beispiel #1
0
 private function _testUploadUnmodified($objectType)
 {
     $objectTypePlural = API::getPluralObjectType($objectType);
     switch ($objectType) {
         case 'collection':
             $data = API::createCollection("Name", false, $this, 'jsonData');
             break;
         case 'item':
             $data = API::createItem("book", array("title" => "Title"), $this, 'jsonData');
             break;
         case 'search':
             $data = API::createSearch("Name", 'default', $this, 'jsonData');
             break;
     }
     $version = $data['version'];
     $this->assertNotEquals(0, $version);
     $response = API::userPut(self::$config['userID'], "{$objectTypePlural}/{$data['key']}", json_encode($data));
     $this->assert204($response);
     $this->assertEquals($version, $response->getHeader("Last-Modified-Version"));
     switch ($objectType) {
         case 'collection':
             $json = API::getCollection($data['key'], $this, 'json');
             break;
         case 'item':
             $json = API::getItem($data['key'], $this, 'json');
             break;
         case 'search':
             $json = API::getSearch($data['key'], $this, 'json');
             break;
     }
     $this->assertEquals($version, $json['version']);
 }
Beispiel #2
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 #3
0
	public function testDateModified() {
		// In case this is ever extended to other objects
		$objectType = 'item';
		$objectTypePlural = API::getPluralObjectType($objectType);
		
		switch ($objectType) {
		case 'item':
			$itemData = array(
				"title" => "Test"
			);
			$json = API::createItem("videoRecording", $itemData, $this, 'jsonData');
			break;
		}
		
		$objectKey = $json['key'];
		$dateModified1 = $json['dateModified'];
		
		// Make sure we're in the next second
		sleep(1);
		
		//
		// If no explicit dateModified, use current timestamp
		//
		$json['title'] = "Test 2";
		unset($json['dateModified']);
		$response = API::userPut(
			self::$config['userID'],
			"$objectTypePlural/$objectKey",
			json_encode($json)
		);
		$this->assert204($response);
		
		switch ($objectType) {
		case 'item':
			$json = API::getItem($objectKey, $this, 'json')['data'];
			break;
		}
		
		$dateModified2 = $json['dateModified'];
		$this->assertNotEquals($dateModified1, $dateModified2);
		
		// Make sure we're in the next second
		sleep(1);
		
		//
		// If existing dateModified, use current timestamp
		//
		$json['title'] = "Test 3";
		$json['dateModified'] = trim(preg_replace("/[TZ]/", " ", $dateModified2));
		$response = API::userPut(
			self::$config['userID'],
			"$objectTypePlural/$objectKey",
			json_encode($json)
		);
		$this->assert204($response);
		
		switch ($objectType) {
		case 'item':
			$json = API::getItem($objectKey, $this, 'json')['data'];
			break;
		}
		
		$dateModified3 = $json['dateModified'];
		$this->assertNotEquals($dateModified2, $dateModified3);
		
		//
		// If explicit dateModified, use that
		//
		$newDateModified = "2013-03-03T21:33:53Z";
		$json['title'] = "Test 4";
		$json['dateModified'] = $newDateModified;
		$response = API::userPut(
			self::$config['userID'],
			"$objectTypePlural/$objectKey",
			json_encode($json)
		);
		$this->assert204($response);
		
		switch ($objectType) {
		case 'item':
			$json = API::getItem($objectKey, $this, 'json')['data'];
			break;
		}
		$dateModified4 = $json['dateModified'];
		$this->assertEquals($newDateModified, $dateModified4);
	}
Beispiel #4
0
 private function _testMultiObjectWriteInvalidObject($objectType)
 {
     $objectTypePlural = API::getPluralObjectType($objectType);
     $response = API::userPost(self::$config['userID'], "{$objectTypePlural}", json_encode(["foo" => "bar"]), array("Content-Type: application/json"));
     $this->assert400($response, "Uploaded data must be a JSON array");
     $response = API::userPost(self::$config['userID'], "{$objectTypePlural}", json_encode([[], ""]), array("Content-Type: application/json"));
     $this->assert400ForObject($response, "Invalid value for index 0 in uploaded data; expected JSON {$objectType} object");
     $this->assert400ForObject($response, "Invalid value for index 1 in uploaded data; expected JSON {$objectType} object", 1);
 }