Source of file DefaultQueryHandlerTest.php
Size: 10,350 Bytes - Last Modified: 2021-12-24T06:41:16+00:00
/var/www/docs.ssmods.com/process/src/tests/QueryHandlers/DefaultQueryHandlerTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 | <?php namespace Colymba\RESTfulAPI\Tests\QueryHandlers; use Colymba\RESTfulAPI\QueryHandlers\DefaultQueryHandler; use SilverStripe\Core\Injector\Injector; use Colymba\RESTfulAPI\RESTfulAPIError; use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Config\Config; use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; use Colymba\RESTfulAPI\Tests\Fixtures\ApiTestAuthor; use Colymba\RESTfulAPI\Tests\Fixtures\ApiTestBook; use Colymba\RESTfulAPI\Tests\Fixtures\ApiTestLibrary; use Colymba\RESTfulAPI\Tests\Fixtures\ApiTestProduct; use ApiTestWidget; use Colymba\RESTfulAPI\Tests\RESTfulAPITester; /** * Default Query Handler Test suite * * @author Thierry Francois @colymba thierry@colymba.com * @copyright Copyright (c) 2013, Thierry Francois * * @license http://opensource.org/licenses/BSD-3-Clause BSD Simplified * * @package RESTfulAPI * @subpackage Tests */ class DefaultQueryHandlerTest extends RESTfulAPITester { protected static $extra_dataobjects = array( ApiTestAuthor::class, ApiTestBook::class, ApiTestLibrary::class, ApiTestProduct::class, ); protected $url_pattern = 'api/$ModelReference/$ID'; /** * Turn on API access for the book and widget fixtures by default */ public function setUp() { parent::setUp(); Config::inst()->update(ApiTestBook::class, 'api_access', true); Config::inst()->update(ApiTestWidget::class, 'api_access', true); $widget = ApiTestWidget::create(['Name' => 'TestWidget1']); $widget->write(); $widget = ApiTestWidget::create(['Name' => 'TestWidget2']); $widget->write(); } protected function getHTTPRequest($method = 'GET', $class = ApiTestBook::class, $id = '', $params = array()) { $request = new HTTPRequest( $method, 'api/' . $class . '/' . $id, $params ); $request->match($this->url_pattern); $request->setRouteParams(array( 'Controller' => 'RESTfulAPI', )); return $request; } protected function getQueryHandler() { $injector = new Injector(); $qh = new DefaultQueryHandler(); $injector->inject($qh); return $qh; } public static function setUpBeforeClass() { parent::setUpBeforeClass(); $product = ApiTestProduct::create(array( 'Title' => 'Sold out product', 'Soldout' => true, )); $product->write(); } /* ********************************************************** * TESTS * */ /** * Checks that query parameters are parsed properly */ public function testQueryParametersParsing() { $qh = $this->getQueryHandler(); $request = $this->getHTTPRequest('GET', ApiTestBook::class, '1', array('Title__StartsWith' => 'K')); $params = $qh->parseQueryParameters($request->getVars()); $params = array_shift($params); $this->assertEquals( $params['Column'], 'Title', 'Column parameter name mismatch' ); $this->assertEquals( $params['Value'], 'K', 'Value parameter mismatch' ); $this->assertEquals( $params['Modifier'], 'StartsWith', 'Modifier parameter mismatch' ); } /** * Checks that access to DataObject with api_access config disabled return error */ public function testAPIDisabled() { Config::inst()->update(ApiTestBook::class, 'api_access', false); $qh = $this->getQueryHandler(); $request = $this->getHTTPRequest('GET', ApiTestBook::class, '1'); $result = $qh->handleQuery($request); $this->assertContainsOnlyInstancesOf( RESTfulAPIError::class, array($result), 'Request for DataObject with api_access set to false should return a RESTfulAPIError' ); } /** * Checks single record requests */ public function testFindSingleModel() { $qh = $this->getQueryHandler(); $request = $this->getHTTPRequest('GET', ApiTestBook::class, '1'); $result = $qh->handleQuery($request); $this->assertContainsOnlyInstancesOf( ApiTestBook::class, array($result), 'Single model request should return a DataObject of class model' ); $this->assertEquals( 1, $result->ID, 'IDs mismatch. DataObject is not the record requested' ); } /** * Checks multiple records requests */ public function testFindMultipleModels() { $qh = $this->getQueryHandler(); $request = $this->getHTTPRequest('GET', ApiTestBook::class); $result = $qh->handleQuery($request); $this->assertContainsOnlyInstancesOf( DataList::class, array($result), 'Request for multiple models should return a DataList' ); $this->assertGreaterThan( 1, $result->toArray(), 'Request should return more than 1 result' ); } /** * Checks fallback for models without explicit mapping */ public function testModelMappingFallback() { $qh = $this->getQueryHandler(); $request = $this->getHTTPRequest('GET', ApiTestWidget::class, '1'); $result = $qh->handleQuery($request); $this->assertContainsOnlyInstancesOf( ApiTestWidget::class, array($result), 'Unmapped model should fall back to standard mapping' ); } /** * Checks max record limit config */ public function testMaxRecordsLimit() { Config::inst()->update(DefaultQueryHandler::class, 'max_records_limit', 1); $qh = $this->getQueryHandler(); $request = $this->getHTTPRequest('GET', ApiTestBook::class); $result = $qh->handleQuery($request); $this->assertCount( 1, $result->toArray(), 'Request for multiple models should implement limit set by max_records_limit config' ); } /** * Checks new record creation */ public function testCreateModel() { $existingRecords = ApiTestBook::get()->toArray(); $qh = $this->getQueryHandler(); $request = $this->getHTTPRequest('POST', ApiTestBook::class); $body = json_encode(array('Title' => 'New Test Book')); $request->setBody($body); $result = $qh->createModel(ApiTestBook::class, $request); $rewRecords = ApiTestBook::get()->toArray(); $this->assertContainsOnlyInstancesOf( DataObject::class, array($result), 'Create model should return a DataObject' ); $this->assertEquals( count($existingRecords) + 1, count($rewRecords), 'Create model should create a database entry' ); $this->assertEquals( 'New Test Book', $result->Title, "Created model title doesn't match" ); // failing tests return error? } /** * Checks new record creation */ public function testModelValidation() { $qh = $this->getQueryHandler(); $request = $this->getHTTPRequest('POST', ApiTestBook::class); $body = json_encode(array('Title' => 'New Test Book', 'Pages' => 101)); $request->setBody($body); $result = $qh->createModel(ApiTestBook::class, $request); $this->assertEquals( 'Too many pages', $result->message, "Model with validation error should return the validation error" ); } /** * Checks record update */ public function testUpdateModel() { $firstRecord = ApiTestBook::get()->first(); $qh = $this->getQueryHandler(); $request = $this->getHTTPRequest('PUT', ApiTestBook::class); $newTitle = $firstRecord->Title . ' UPDATED'; $body = json_encode(array('Title' => $newTitle)); $request->setBody($body); $result = $qh->updateModel(ApiTestBook::class, $firstRecord->ID, $request); $updatedRecord = DataObject::get_by_id(ApiTestBook::class, $firstRecord->ID); $this->assertContainsOnlyInstancesOf( DataObject::class, array($result), 'Update model should return a DataObject' ); $this->assertEquals( $newTitle, $updatedRecord->Title, "Update model didn't update database record" ); // failing tests return error? } /** * Checks record deletion */ public function testDeleteModel() { $firstRecord = ApiTestBook::get()->first(); $qh = $this->getQueryHandler(); $request = $this->getHTTPRequest('DELETE', ApiTestBook::class); $result = $qh->deleteModel(ApiTestBook::class, $firstRecord->ID, $request); $deletedRecord = DataObject::get_by_id(ApiTestBook::class, $firstRecord->ID); $this->assertNull( $deletedRecord, 'Delete model should delete a database record' ); } public function testAfterDeserialize() { $product = ApiTestProduct::get()->first(); $qh = $this->getQueryHandler(); $request = $this->getHTTPRequest('PUT', ApiTestProduct::class, $product->ID); $body = json_encode(array( 'Title' => 'Making product available', 'Soldout' => false, )); $request->setBody($body); $updatedProduct = $qh->handleQuery($request); $this->assertContainsOnlyInstancesOf( DataObject::class, array($updatedProduct), 'Update model should return a DataObject' ); $this->assertEquals( ApiTestProduct::$rawJSON, $body, "Raw JSON passed into 'onBeforeDeserialize' should match request payload" ); $this->assertTrue( $updatedProduct->Soldout == 1, "Product should still be sold out, because 'onAfterDeserialize' unset the data bafore writing" ); } } |