api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tests.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. if (!file_exists(__DIR__.'/config.php')) {
  3. copy(__DIR__.'/config.php.dist',__DIR__.'/config.php');
  4. }
  5. require __DIR__.'/config.php';
  6. require __DIR__.'/../api.php';
  7. class API
  8. {
  9. protected $test;
  10. protected $api;
  11. public function __construct($test)
  12. {
  13. $this->test = $test;
  14. }
  15. private function action($method,$url,$data='')
  16. {
  17. $url = parse_url($url);
  18. $query = isset($url['query'])?$url['query']:'';
  19. parse_str($query,$get);
  20. $this->api = new PHP_CRUD_API(array(
  21. 'dbengine'=>PHP_CRUD_API_Config::$dbengine,
  22. 'hostname'=>PHP_CRUD_API_Config::$hostname,
  23. 'username'=>PHP_CRUD_API_Config::$username,
  24. 'password'=>PHP_CRUD_API_Config::$password,
  25. 'database'=>PHP_CRUD_API_Config::$database,
  26. // callbacks
  27. 'table_authorizer'=>function($action,$database,$table) { return true; },
  28. 'column_authorizer'=>function($action,$database,$table,$column) { return !($column=='password'&&$action=='list'); },
  29. 'record_filter'=>function($action,$database,$table) { return ($table=='posts')?array('id,neq,13'):false; },
  30. 'tenancy_function'=>function($action,$database,$table,$column) { return ($table=='users'&&$column=='id')?1:null; },
  31. 'input_sanitizer'=>function($action,$database,$table,$column,$type,$value) { return $value===null?null:strip_tags($value); },
  32. 'input_validator'=>function($action,$database,$table,$column,$type,$value,$context) { return ($column=='category_id' && !is_numeric($value))?'must be numeric':true; },
  33. // for tests
  34. 'method' =>$method,
  35. 'request' =>$url['path'],
  36. 'post'=>$data,
  37. 'get' =>$get,
  38. ));
  39. return $this;
  40. }
  41. public function get($url)
  42. {
  43. return $this->action('GET',$url);
  44. }
  45. public function post($url,$data)
  46. {
  47. return $this->action('POST',$url,$data);
  48. }
  49. public function put($url,$data)
  50. {
  51. return $this->action('PUT',$url,$data);
  52. }
  53. public function delete($url)
  54. {
  55. return $this->action('DELETE',$url);
  56. }
  57. public function options($url)
  58. {
  59. return $this->action('OPTIONS',$url);
  60. }
  61. public function expectAny()
  62. {
  63. ob_start();
  64. $this->api->executeCommand();
  65. ob_end_clean();
  66. return $this;
  67. }
  68. public function expect($output,$error=false)
  69. {
  70. $exception = false;
  71. ob_start();
  72. try {
  73. $this->api->executeCommand();
  74. } catch (\Exception $e) {
  75. $exception = $e->getMessage();
  76. }
  77. $data = ob_get_contents();
  78. ob_end_clean();
  79. if ($exception) $this->test->assertEquals($error, $exception);
  80. else $this->test->assertEquals($output, $data);
  81. return $this;
  82. }
  83. }
  84. class PHP_CRUD_API_Test extends PHPUnit_Framework_TestCase
  85. {
  86. public static function setUpBeforeClass()
  87. {
  88. if (PHP_CRUD_API_Config::$database=='{{test_database}}') {
  89. die("Configure database in 'config.php' before running tests.\n");
  90. }
  91. $dbengine = PHP_CRUD_API_Config::$dbengine;
  92. $hostname = PHP_CRUD_API_Config::$hostname;
  93. $username = PHP_CRUD_API_Config::$username;
  94. $password = PHP_CRUD_API_Config::$password;
  95. $database = PHP_CRUD_API_Config::$database;
  96. $fixture = __DIR__.'/blog_'.strtolower($dbengine).'.sql';
  97. if ($dbengine == 'MySQL') {
  98. $link = mysqli_connect($hostname, $username, $password, $database);
  99. if (mysqli_connect_errno()) {
  100. die("Connect failed: ".mysqli_connect_error()."\n");
  101. }
  102. mysqli_set_charset($link,'utf8');
  103. $i=0;
  104. if (mysqli_multi_query($link, file_get_contents($fixture))) {
  105. do { $i++; mysqli_next_result($link); } while (mysqli_more_results($link));
  106. }
  107. if (mysqli_errno($link)) {
  108. die("Loading '$fixture' failed on statemement #$i with error:\n".mysqli_error($link)."\n");
  109. }
  110. mysqli_close($link);
  111. } elseif ($dbengine == 'SQLServer') {
  112. $connectionInfo = array();
  113. $connectionInfo['UID']=$username;
  114. $connectionInfo['PWD']=$password;
  115. $connectionInfo['Database']=$database;
  116. $connectionInfo['CharacterSet']='UTF-8';
  117. $conn = sqlsrv_connect( $hostname, $connectionInfo);
  118. if (!$conn) {
  119. die("Connect failed: ".print_r( sqlsrv_errors(), true));
  120. }
  121. $queries = preg_split('/\n\s*GO\s*\n/', file_get_contents($fixture));
  122. array_pop($queries);
  123. foreach ($queries as $i=>$query) {
  124. if (!sqlsrv_query($conn, $query)) {
  125. $i++;
  126. die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( sqlsrv_errors(), true)."\n");
  127. }
  128. }
  129. sqlsrv_close($conn);
  130. } elseif ($dbengine == 'PostgreSQL') {
  131. $e = function ($v) { return str_replace(array('\'','\\'),array('\\\'','\\\\'),$v); };
  132. $hostname = $e($hostname);
  133. $database = $e($database);
  134. $username = $e($username);
  135. $password = $e($password);
  136. $conn_string = "host='$hostname' dbname='$database' user='$username' password='$password' options='--client_encoding=UTF8'";
  137. $db = pg_connect($conn_string);
  138. if (!$db) {
  139. die("Connect failed: ". pg_last_error());
  140. }
  141. $queries = preg_split('/;\s*\n/', file_get_contents($fixture));
  142. array_pop($queries);
  143. foreach ($queries as $i=>$query) {
  144. if (!pg_query($db, $query.';')) {
  145. $i++;
  146. die("Loading '$fixture' failed on statemement #$i with error:\n".print_r( pg_last_error($db), true)."\n");
  147. }
  148. }
  149. pg_close($db);
  150. } elseif ($dbengine == 'SQLite') {
  151. $db = new SQLite3($database);
  152. if (!$db) {
  153. die("Could not open '$database' SQLite database: ".SQLite3::lastErrorMsg().' ('.SQLite3::lastErrorCode().")\n");
  154. }
  155. $queries = preg_split('/;\s*\n/', file_get_contents($fixture));
  156. array_pop($queries);
  157. foreach ($queries as $i=>$query) {
  158. if (!$db->query($query.';')) {
  159. $i++;
  160. die("Loading '$fixture' failed on statemement #$i with error:\n".$db->lastErrorCode().': '.$db->lastErrorMsg()."\n");
  161. }
  162. }
  163. $db->close();
  164. }
  165. }
  166. public function testListPosts()
  167. {
  168. $test = new API($this);
  169. $test->get('/posts');
  170. $test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[[1,1,1,"blog started"],[2,1,2,"It works!"]]}}');
  171. }
  172. public function testListPostColumns()
  173. {
  174. $test = new API($this);
  175. $test->get('/posts?columns=id,content');
  176. $test->expect('{"posts":{"columns":["id","content"],"records":[[1,"blog started"],[2,"It works!"]]}}');
  177. }
  178. public function testListPostsWithTransform()
  179. {
  180. $test = new API($this);
  181. $test->get('/posts?transform=1');
  182. $test->expect('{"posts":[{"id":1,"user_id":1,"category_id":1,"content":"blog started"},{"id":2,"user_id":1,"category_id":2,"content":"It works!"}]}');
  183. }
  184. public function testReadPost()
  185. {
  186. $test = new API($this);
  187. $test->get('/posts/2');
  188. $test->expect('{"id":2,"user_id":1,"category_id":2,"content":"It works!"}');
  189. }
  190. public function testReadPosts()
  191. {
  192. $test = new API($this);
  193. $test->get('/posts/1,2');
  194. $test->expect('[{"id":1,"user_id":1,"category_id":1,"content":"blog started"},{"id":2,"user_id":1,"category_id":2,"content":"It works!"}]');
  195. }
  196. public function testReadPostColumns()
  197. {
  198. $test = new API($this);
  199. $test->get('/posts/2?columns=id,content');
  200. $test->expect('{"id":2,"content":"It works!"}');
  201. }
  202. public function testAddPost()
  203. {
  204. $test = new API($this);
  205. $test->post('/posts','{"user_id":1,"category_id":1,"content":"test"}');
  206. $test->expect('3');
  207. }
  208. public function testEditPost()
  209. {
  210. $test = new API($this);
  211. $test->put('/posts/3','{"user_id":1,"category_id":1,"content":"test (edited)"}');
  212. $test->expect('1');
  213. $test->get('/posts/3');
  214. $test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited)"}');
  215. }
  216. public function testEditPostColumnsMissingField()
  217. {
  218. $test = new API($this);
  219. $test->put('/posts/3?columns=id,content','{"content":"test (edited 2)"}');
  220. $test->expect('1');
  221. $test->get('/posts/3');
  222. $test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited 2)"}');
  223. }
  224. public function testEditPostColumnsExtraField()
  225. {
  226. $test = new API($this);
  227. $test->put('/posts/3?columns=id,content','{"user_id":2,"content":"test (edited 3)"}');
  228. $test->expect('1');
  229. $test->get('/posts/3');
  230. $test->expect('{"id":3,"user_id":1,"category_id":1,"content":"test (edited 3)"}');
  231. }
  232. public function testEditPostWithUtf8Content()
  233. {
  234. $utf8 = json_encode('Hello world, Καλημέρα κόσμε, コンニチハ');
  235. $test = new API($this);
  236. $test->put('/posts/2','{"content":'.$utf8.'}');
  237. $test->expect('1');
  238. $test->get('/posts/2');
  239. $test->expect('{"id":2,"user_id":1,"category_id":2,"content":'.$utf8.'}');
  240. }
  241. public function testEditPostWithUtf8ContentWithPost()
  242. {
  243. $utf8 = '€ Hello world, Καλημέρα κόσμε, コンニチハ';
  244. $url_encoded = urlencode($utf8);
  245. $json_encoded = json_encode($utf8);
  246. $test = new API($this);
  247. $test->put('/posts/2','content='.$url_encoded);
  248. $test->expect('1');
  249. $test->get('/posts/2');
  250. $test->expect('{"id":2,"user_id":1,"category_id":2,"content":'.$json_encoded.'}');
  251. }
  252. public function testDeletePost()
  253. {
  254. $test = new API($this);
  255. $test->delete('/posts/3');
  256. $test->expect('1');
  257. $test->get('/posts/3');
  258. $test->expect(false,'Not found (object)');
  259. }
  260. public function testAddPostWithPost()
  261. {
  262. $test = new API($this);
  263. $test->post('/posts','user_id=1&category_id=1&content=test');
  264. $test->expect('4');
  265. }
  266. public function testEditPostWithPost()
  267. {
  268. $test = new API($this);
  269. $test->put('/posts/4','user_id=1&category_id=1&content=test+(edited)');
  270. $test->expect('1');
  271. $test->get('/posts/4');
  272. $test->expect('{"id":4,"user_id":1,"category_id":1,"content":"test (edited)"}');
  273. }
  274. public function testDeletePostWithPost()
  275. {
  276. $test = new API($this);
  277. $test->delete('/posts/4');
  278. $test->expect('1');
  279. $test->get('/posts/4');
  280. $test->expect(false,'Not found (object)');
  281. }
  282. public function testListWithPaginate()
  283. {
  284. $test = new API($this);
  285. for ($i=1;$i<=10;$i++) {
  286. $test->post('/posts','{"user_id":1,"category_id":1,"content":"#'.$i.'"}');
  287. $test->expect(4+$i);
  288. }
  289. $test->get('/posts?page=2,2&order=id');
  290. $test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[[5,1,1,"#1"],[6,1,1,"#2"]],"results":11}}');
  291. }
  292. public function testListWithPaginateLastPage()
  293. {
  294. $test = new API($this);
  295. $test->get('/posts?page=3,5&order=id');
  296. $test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[[14,1,1,"#10"]],"results":11}}');
  297. }
  298. public function testListExampleFromReadme()
  299. {
  300. $test = new API($this);
  301. $test->get('/posts?include=categories,tags,comments&filter=id,eq,1');
  302. $test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[[1,1,1,"blog started"]]},"post_tags":{"relations":{"post_id":"posts.id"},"columns":["id","post_id","tag_id"],"records":[[1,1,1],[2,1,2]]},"categories":{"relations":{"id":"posts.category_id"},"columns":["id","name","icon"],"records":[[1,"announcement",null]]},"tags":{"relations":{"id":"post_tags.tag_id"},"columns":["id","name"],"records":[[1,"funny"],[2,"important"]]},"comments":{"relations":{"post_id":"posts.id"},"columns":["id","post_id","message"],"records":[[1,1,"great"],[2,1,"fantastic"]]}}');
  303. }
  304. public function testListExampleFromReadmeWithTransform()
  305. {
  306. $test = new API($this);
  307. $test->get('/posts?include=categories,tags,comments&filter=id,eq,1&transform=1');
  308. $test->expect('{"posts":[{"id":1,"post_tags":[{"id":1,"post_id":1,"tag_id":1,"tags":[{"id":1,"name":"funny"}]},{"id":2,"post_id":1,"tag_id":2,"tags":[{"id":2,"name":"important"}]}],"comments":[{"id":1,"post_id":1,"message":"great"},{"id":2,"post_id":1,"message":"fantastic"}],"user_id":1,"category_id":1,"categories":[{"id":1,"name":"announcement","icon":null}],"content":"blog started"}]}');
  309. }
  310. public function testEditCategoryWithBinaryContent()
  311. {
  312. $binary = base64_encode("\0abc\0\n\r\b\0");
  313. $base64url = rtrim(strtr($binary, '+/', '-_'), '=');
  314. $test = new API($this);
  315. $test->put('/categories/2','{"icon":"'.$base64url.'"}');
  316. $test->expect('1');
  317. $test->get('/categories/2');
  318. $test->expect('{"id":2,"name":"article","icon":"'.$binary.'"}');
  319. }
  320. public function testEditCategoryWithNull()
  321. {
  322. $test = new API($this);
  323. $test->put('/categories/2','{"icon":null}');
  324. $test->expect('1');
  325. $test->get('/categories/2');
  326. $test->expect('{"id":2,"name":"article","icon":null}');
  327. }
  328. public function testEditCategoryWithBinaryContentWithPost()
  329. {
  330. $binary = base64_encode("€ \0abc\0\n\r\b\0");
  331. $base64url = rtrim(strtr($binary, '+/', '-_'), '=');
  332. $test = new API($this);
  333. $test->put('/categories/2','icon='.$base64url);
  334. $test->expect('1');
  335. $test->get('/categories/2');
  336. $test->expect('{"id":2,"name":"article","icon":"'.$binary.'"}');
  337. }
  338. public function testListCategoriesWithBinaryContent()
  339. {
  340. $test = new API($this);
  341. $test->get('/categories');
  342. $test->expect('{"categories":{"columns":["id","name","icon"],"records":[[1,"announcement",null],[2,"article","4oKsIABhYmMACg1cYgA="]]}}');
  343. }
  344. public function testEditCategoryWithNullWithPost()
  345. {
  346. $test = new API($this);
  347. $test->put('/categories/2','icon__is_null');
  348. $test->expect('1');
  349. $test->get('/categories/2');
  350. $test->expect('{"id":2,"name":"article","icon":null}');
  351. }
  352. public function testAddPostFailure()
  353. {
  354. $test = new API($this);
  355. $test->post('/posts','{"user_id":"a","category_id":1,"content":"tests"}');
  356. $test->expect('null');
  357. }
  358. public function testOptionsRequest()
  359. {
  360. $test = new API($this);
  361. $test->options('/posts/2');
  362. $test->expect('["Access-Control-Allow-Headers: Content-Type","Access-Control-Allow-Methods: OPTIONS, GET, PUT, POST, DELETE","Access-Control-Allow-Credentials: true","Access-Control-Max-Age: 1728000"]',false);
  363. }
  364. public function testHidingPasswordColumn()
  365. {
  366. $test = new API($this);
  367. $test->get('/users?filter=id,eq,1&transform=1');
  368. $test->expect('{"users":[{"id":1,"username":"user1","location":null}]}');
  369. }
  370. public function testValidatorErrorMessage()
  371. {
  372. $test = new API($this);
  373. $test->put('/posts/1','{"category_id":"a"}');
  374. $test->expect(false,'{"category_id":"must be numeric"}');
  375. }
  376. public function testSanitizerToStripTags()
  377. {
  378. $test = new API($this);
  379. $test->put('/categories/2','{"name":"<script>alert();</script>"}');
  380. $test->expect('1');
  381. $test->get('/categories/2');
  382. $test->expect('{"id":2,"name":"alert();","icon":null}');
  383. }
  384. public function testErrorOnInvalidJson()
  385. {
  386. $test = new API($this);
  387. $test->post('/posts','{"}');
  388. $test->expect(false,'Not found (input)');
  389. }
  390. public function testErrorOnDuplicatePrimaryKey()
  391. {
  392. $test = new API($this);
  393. $test->post('/posts','{"id":1,"user_id":1,"category_id":1,"content":"blog started (duplicate)"}');
  394. $test->expect('null');
  395. }
  396. public function testErrorOnFailingForeignKeyConstraint()
  397. {
  398. $test = new API($this);
  399. $test->post('/posts','{"user_id":3,"category_id":1,"content":"fk constraint"}');
  400. $test->expect('null');
  401. }
  402. public function testMissingIntermediateTable()
  403. {
  404. $test = new API($this);
  405. $test->get('/users?include=posts,tags');
  406. $test->expect('{"users":{"columns":["id","username","location"],"records":[[1,"user1",null]]},"posts":{"relations":{"user_id":"users.id"},"columns":["id","user_id","category_id","content"],"records":[[1,1,1,"blog started"],[2,1,2,"\u20ac Hello world, \u039a\u03b1\u03bb\u03b7\u03bc\u1f73\u03c1\u03b1 \u03ba\u1f79\u03c3\u03bc\u03b5, \u30b3\u30f3\u30cb\u30c1\u30cf"],[5,1,1,"#1"],[6,1,1,"#2"],[7,1,1,"#3"],[8,1,1,"#4"],[9,1,1,"#5"],[10,1,1,"#6"],[11,1,1,"#7"],[12,1,1,"#8"],[14,1,1,"#10"]]},"post_tags":{"relations":{"post_id":"posts.id"},"columns":["id","post_id","tag_id"],"records":[[1,1,1],[2,1,2],[3,2,1],[4,2,2]]},"tags":{"relations":{"id":"post_tags.tag_id"},"columns":["id","name"],"records":[[1,"funny"],[2,"important"]]}}');
  407. }
  408. public function testEditUserPassword()
  409. {
  410. $test = new API($this);
  411. $test->put('/users/1','{"password":"testtest"}');
  412. $test->expect('1');
  413. }
  414. public function testEditUserLocation()
  415. {
  416. $test = new API($this);
  417. $test->put('/users/1','{"location":"POINT(30 20)"}');
  418. $test->expect('1');
  419. $test->get('/users/1?columns=id,location');
  420. if (PHP_CRUD_API_Config::$dbengine=='SQLServer') {
  421. $test->expect('{"id":1,"location":"POINT (30 20)"}');
  422. } else {
  423. $test->expect('{"id":1,"location":"POINT(30 20)"}');
  424. }
  425. }
  426. public function testListUserLocations()
  427. {
  428. $test = new API($this);
  429. $test->get('/users?columns=id,location');
  430. if (PHP_CRUD_API_Config::$dbengine=='SQLServer') {
  431. $test->expect('{"users":{"columns":["id","location"],"records":[[1,"POINT (30 20)"]]}}');
  432. } else {
  433. $test->expect('{"users":{"columns":["id","location"],"records":[[1,"POINT(30 20)"]]}}');
  434. }
  435. }
  436. public function testEditUserWithId()
  437. {
  438. if (PHP_CRUD_API_Config::$dbengine!='SQLServer') {
  439. $test = new API($this);
  440. $test->put('/users/1','{"id":2,"password":"testtest2"}');
  441. $test->expect('1');
  442. $test->get('/users/1?columns=id,username,password');
  443. $test->expect('{"id":1,"username":"user1","password":"testtest2"}');
  444. }
  445. }
  446. public function testReadOtherUser()
  447. {
  448. $test = new API($this);
  449. $test->get('/users/2');
  450. $test->expect(false,'Not found (object)');
  451. }
  452. public function testEditOtherUser()
  453. {
  454. $test = new API($this);
  455. $test->put('/users/2','{"password":"testtest"}');
  456. $test->expect('0');
  457. }
  458. public function testFilterCategoryOnNullIcon()
  459. {
  460. $test = new API($this);
  461. $test->get('/categories?filter[]=icon,is,null&transform=1');
  462. $test->expect('{"categories":[{"id":1,"name":"announcement","icon":null},{"id":2,"name":"alert();","icon":null}]}');
  463. }
  464. public function testFilterCategoryOnNotNullIcon()
  465. {
  466. $test = new API($this);
  467. $test->get('/categories?filter[]=icon,nis,null&transform=1');
  468. $test->expect('{"categories":[]}');
  469. }
  470. public function testFilterPostsNotIn()
  471. {
  472. $test = new API($this);
  473. $test->get('/posts?filter[]=id,nin,1,2,3,4,7,8,9,10,11,12,13,14&transform=1');
  474. $test->expect('{"posts":[{"id":5,"user_id":1,"category_id":1,"content":"#1"},{"id":6,"user_id":1,"category_id":1,"content":"#2"}]}');
  475. }
  476. public function testFilterPostsBetween()
  477. {
  478. $test = new API($this);
  479. $test->get('/posts?filter[]=id,bt,5,6&transform=1');
  480. $test->expect('{"posts":[{"id":5,"user_id":1,"category_id":1,"content":"#1"},{"id":6,"user_id":1,"category_id":1,"content":"#2"}]}');
  481. }
  482. public function testFilterPostsNotBetween()
  483. {
  484. $test = new API($this);
  485. $test->get('/posts?filter[]=id,nbt,2,13&transform=1');
  486. $test->expect('{"posts":[{"id":1,"user_id":1,"category_id":1,"content":"blog started"},{"id":14,"user_id":1,"category_id":1,"content":"#10"}]}');
  487. }
  488. public function testColumnsWithTable()
  489. {
  490. $test = new API($this);
  491. $test->get('/posts?columns=posts.content&filter=id,eq,1&transform=1');
  492. $test->expect('{"posts":[{"content":"blog started"}]}');
  493. }
  494. public function testColumnsWithTableWildcard()
  495. {
  496. $test = new API($this);
  497. $test->get('/posts?columns=posts.*&filter=id,eq,1&transform=1');
  498. $test->expect('{"posts":[{"id":1,"user_id":1,"category_id":1,"content":"blog started"}]}');
  499. }
  500. public function testColumnsOnInclude()
  501. {
  502. $test = new API($this);
  503. $test->get('/posts?include=categories&columns=categories.name&filter=id,eq,1&transform=1');
  504. $test->expect('{"posts":[{"category_id":1,"categories":[{"id":1,"name":"announcement"}]}]}');
  505. }
  506. public function testFilterOnRelationAnd()
  507. {
  508. $test = new API($this);
  509. $test->get('/categories?include=posts&filter[]=id,ge,1&filter[]=id,le,1&filter[]=id,le,2&filter[]=posts.id,lt,8&filter[]=posts.id,gt,4');
  510. $test->expect('{"categories":{"columns":["id","name","icon"],"records":[[1,"announcement",null]]},"posts":{"relations":{"category_id":"categories.id"},"columns":["id","user_id","category_id","content"],"records":[[5,1,1,"#1"],[6,1,1,"#2"],[7,1,1,"#3"]]}}');
  511. }
  512. public function testFilterOnRelationOr()
  513. {
  514. $test = new API($this);
  515. $test->get('/categories?include=posts&filter[]=id,ge,1&filter[]=id,le,1&filter[]=posts.id,eq,5&filter[]=posts.id,eq,6&filter[]=posts.id,eq,7&satisfy=all,posts.any');
  516. $test->expect('{"categories":{"columns":["id","name","icon"],"records":[[1,"announcement",null]]},"posts":{"relations":{"category_id":"categories.id"},"columns":["id","user_id","category_id","content"],"records":[[5,1,1,"#1"],[6,1,1,"#2"],[7,1,1,"#3"]]}}');
  517. }
  518. public function testColumnsOnWrongInclude()
  519. {
  520. $test = new API($this);
  521. $test->get('/posts?include=categories&columns=categories&filter=id,eq,1&transform=1');
  522. $test->expect('{"posts":[{"category_id":1,"categories":[{"id":1}]}]}');
  523. }
  524. public function testColumnsOnImplicitJoin()
  525. {
  526. $test = new API($this);
  527. $test->get('/posts?include=tags&columns=posts.id,tags.name&filter=id,eq,1&transform=1');
  528. $test->expect('{"posts":[{"id":1,"post_tags":[{"post_id":1,"tag_id":1,"tags":[{"id":1,"name":"funny"}]},{"post_id":1,"tag_id":2,"tags":[{"id":2,"name":"important"}]}]}]}');
  529. }
  530. public function testSpatialFilterWithin()
  531. {
  532. if (PHP_CRUD_API_Config::$dbengine!='SQLite') {
  533. $test = new API($this);
  534. $test->get('/users?columns=id,username&filter=location,swi,POINT(30 20)');
  535. $test->expect('{"users":{"columns":["id","username"],"records":[[1,"user1"]]}}');
  536. }
  537. }
  538. public function testAddPostsWithNonExistingCategory()
  539. {
  540. $test = new API($this);
  541. $test->post('/posts','[{"user_id":1,"category_id":1,"content":"tests"},{"user_id":1,"category_id":15,"content":"tests"}]');
  542. $test->expect('null');
  543. $test->get('/posts?columns=content&filter=content,eq,tests');
  544. $test->expect('{"posts":{"columns":["content"],"records":[]}}');
  545. }
  546. public function testAddPosts()
  547. {
  548. $test = new API($this);
  549. $test->post('/posts','[{"user_id":1,"category_id":1,"content":"tests"},{"user_id":1,"category_id":1,"content":"tests"}]');
  550. $test->expectAny();
  551. $test->get('/posts?columns=content&filter=content,eq,tests');
  552. $test->expect('{"posts":{"columns":["content"],"records":[["tests"],["tests"]]}}');
  553. }
  554. public function testListEvents()
  555. {
  556. $test = new API($this);
  557. $test->get('/events?columns=datetime');
  558. $test->expect('{"events":{"columns":["datetime"],"records":[["2016-01-01 13:01:01.111"]]}}');
  559. }
  560. public function testListTagUsage()
  561. {
  562. $test = new API($this);
  563. $test->get('/tag_usage');
  564. $test->expect('{"tag_usage":{"columns":["name","count"],"records":[["funny",2],["important",2]]}}');
  565. }
  566. public function testUpdateMultipleTags()
  567. {
  568. $test = new API($this);
  569. $test->get('/tags?transform=1');
  570. $test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}');
  571. $test->put('/tags/1,2','[{"name":"funny"},{"name":"important"}]');
  572. $test->expect('[1,1]');
  573. }
  574. public function testUpdateMultipleTagsTooManyIds()
  575. {
  576. $test = new API($this);
  577. $test->put('/tags/1,2,3','[{"name":"funny!!!"},{"name":"important"}]');
  578. $test->expect(false,'Not found (subject)');
  579. $test->get('/tags?transform=1');
  580. $test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}');
  581. }
  582. public function testUpdateMultipleTagsWithoutFields()
  583. {
  584. $test = new API($this);
  585. $test->put('/tags/1,2','[{"name":"funny!!!"},{}]');
  586. $test->expect('null');
  587. $test->get('/tags?transform=1');
  588. $test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}');
  589. }
  590. public function testDeleteMultipleTags()
  591. {
  592. $test = new API($this);
  593. $test->post('/tags','[{"name":"extra"},{"name":"more"}]');
  594. $test->expect('[3,4]');
  595. $test->delete('/tags/3,4');
  596. $test->expect('[1,1]');
  597. $test->get('/tags?transform=1');
  598. $test->expect('{"tags":[{"id":1,"name":"funny"},{"id":2,"name":"important"}]}');
  599. }
  600. public function testListProducts()
  601. {
  602. $test = new API($this);
  603. $test->get('/products?transform=1');
  604. $test->expect('{"products":[{"id":1,"name":"Calculator","price":"23.01"}]}');
  605. }
  606. }