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 testNewSearchConditionErrors()
 {
     $json = API::createSearch("Test", array(array("operator" => "contains", "value" => "test")), $this, 'responseJSON');
     $this->assert400ForObject($json, "'condition' property not provided for search condition");
     $json = API::createSearch("Test", array(array("condition" => "", "operator" => "contains", "value" => "test")), $this, 'responseJSON');
     $this->assert400ForObject($json, "Search condition cannot be empty");
     $json = API::createSearch("Test", array(array("condition" => "title", "value" => "test")), $this, 'responseJSON');
     $this->assert400ForObject($json, "'operator' property not provided for search condition");
     $json = API::createSearch("Test", array(array("condition" => "title", "operator" => "", "value" => "test")), $this, 'responseJSON');
     $this->assert400ForObject($json, "Search operator cannot be empty");
 }
Beispiel #4
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);
     }
 }