VPN licensing server
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

440 rindas
15KB

  1. <?php
  2. class BasicTest extends SimpleTest {
  3. function __construct() {
  4. foreach (DB::tableList() as $table) {
  5. DB::query("DROP TABLE %b", $table);
  6. }
  7. }
  8. function test_1_create_table() {
  9. DB::query("CREATE TABLE `accounts` (
  10. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  11. `profile_id` INT NOT NULL,
  12. `username` VARCHAR( 255 ) NOT NULL ,
  13. `password` VARCHAR( 255 ) NULL ,
  14. `age` INT NOT NULL DEFAULT '10',
  15. `height` DOUBLE NOT NULL DEFAULT '10.0',
  16. `favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi',
  17. `birthday` TIMESTAMP NOT NULL
  18. ) ENGINE = InnoDB");
  19. DB::query("CREATE TABLE `profile` (
  20. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  21. `signature` VARCHAR( 255 ) NULL DEFAULT 'donewriting'
  22. ) ENGINE = InnoDB");
  23. DB::query("CREATE TABLE `fake%s_table` (
  24. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  25. `name` VARCHAR( 255 ) NULL DEFAULT 'blah'
  26. ) ENGINE = InnoDB");
  27. $mysqli = DB::get();
  28. DB::disconnect();
  29. @$this->assert(!$mysqli->server_info);
  30. }
  31. function test_1_5_empty_table() {
  32. $counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
  33. $this->assert($counter === strval(0));
  34. $row = DB::queryFirstRow("SELECT * FROM accounts");
  35. $this->assert($row === null);
  36. $field = DB::queryFirstField("SELECT * FROM accounts");
  37. $this->assert($field === null);
  38. $field = DB::queryOneField('nothere', "SELECT * FROM accounts");
  39. $this->assert($field === null);
  40. $column = DB::queryFirstColumn("SELECT * FROM accounts");
  41. $this->assert(is_array($column) && count($column) === 0);
  42. $column = DB::queryOneColumn('nothere', "SELECT * FROM accounts"); //TODO: is this what we want?
  43. $this->assert(is_array($column) && count($column) === 0);
  44. }
  45. function test_2_insert_row() {
  46. $true = DB::insert('accounts', array(
  47. 'username' => 'Abe',
  48. 'password' => 'hello'
  49. ));
  50. $this->assert($true === true);
  51. $this->assert(DB::affectedRows() === 1);
  52. $counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
  53. $this->assert($counter === strval(1));
  54. }
  55. function test_3_more_inserts() {
  56. DB::insert('`accounts`', array(
  57. 'username' => 'Bart',
  58. 'password' => 'hello',
  59. 'age' => 15,
  60. 'height' => 10.371
  61. ));
  62. $dbname = DB::$dbName;
  63. DB::insert("`$dbname`.`accounts`", array(
  64. 'username' => 'Charlie\'s Friend',
  65. 'password' => 'goodbye',
  66. 'age' => 30,
  67. 'height' => 155.23,
  68. 'favorite_word' => null,
  69. ));
  70. $this->assert(DB::insertId() === 3);
  71. $counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
  72. $this->assert($counter === strval(3));
  73. DB::insert('`accounts`', array(
  74. 'username' => 'Deer',
  75. 'password' => '',
  76. 'age' => 15,
  77. 'height' => 10.371
  78. ));
  79. $username = DB::queryFirstField("SELECT username FROM accounts WHERE password=%s0", null);
  80. $this->assert($username === 'Deer');
  81. $password = DB::queryFirstField("SELECT password FROM accounts WHERE favorite_word IS NULL");
  82. $this->assert($password === 'goodbye');
  83. DB::$usenull = false;
  84. DB::insertUpdate('accounts', array(
  85. 'id' => 3,
  86. 'favorite_word' => null,
  87. ));
  88. $password = DB::queryFirstField("SELECT password FROM accounts WHERE favorite_word=%s AND favorite_word=%s", null, '');
  89. $this->assert($password === 'goodbye');
  90. DB::$usenull = true;
  91. DB::insertUpdate('accounts', array(
  92. 'id' => 3,
  93. 'favorite_word' => null,
  94. ));
  95. DB::$param_char = '###';
  96. $bart = DB::queryFirstRow("SELECT * FROM accounts WHERE age IN ###li AND height IN ###ld AND username IN ###ls",
  97. array(15, 25), array(10.371, 150.123), array('Bart', 'Barts'));
  98. $this->assert($bart['username'] === 'Bart');
  99. DB::insert('accounts', array('username' => 'f_u'));
  100. DB::query("DELETE FROM accounts WHERE username=###s", 'f_u');
  101. DB::$param_char = '%';
  102. $charlie_password = DB::queryFirstField("SELECT password FROM accounts WHERE username IN %ls AND username = %s",
  103. array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend');
  104. $this->assert($charlie_password === 'goodbye');
  105. $charlie_password = DB::queryOneField('password', "SELECT * FROM accounts WHERE username IN %ls AND username = %s",
  106. array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend');
  107. $this->assert($charlie_password === 'goodbye');
  108. $passwords = DB::queryFirstColumn("SELECT password FROM accounts WHERE username=%s", 'Bart');
  109. $this->assert(count($passwords) === 1);
  110. $this->assert($passwords[0] === 'hello');
  111. $username = $password = $age = null;
  112. list($age, $username, $password) = DB::queryOneList("SELECT age,username,password FROM accounts WHERE username=%s", 'Bart');
  113. $this->assert($username === 'Bart');
  114. $this->assert($password === 'hello');
  115. $this->assert($age == 15);
  116. $mysqli_result = DB::queryRaw("SELECT * FROM accounts WHERE favorite_word IS NULL");
  117. $this->assert($mysqli_result instanceof MySQLi_Result);
  118. $row = $mysqli_result->fetch_assoc();
  119. $this->assert($row['password'] === 'goodbye');
  120. $this->assert($mysqli_result->fetch_assoc() === null);
  121. }
  122. function test_4_query() {
  123. DB::update('accounts', array(
  124. 'birthday' => new DateTime('10 September 2000 13:13:13')
  125. ), 'username=%s', 'Charlie\'s Friend');
  126. $results = DB::query("SELECT * FROM accounts WHERE username=%s AND birthday IN %lt", 'Charlie\'s Friend', array('September 10 2000 13:13:13'));
  127. $this->assert(count($results) === 1);
  128. $this->assert($results[0]['age'] === '30' && $results[0]['password'] === 'goodbye');
  129. $this->assert($results[0]['birthday'] == '2000-09-10 13:13:13');
  130. $results = DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
  131. $this->assert(count($results) === 3);
  132. $columnlist = DB::columnList('accounts');
  133. $this->assert(count($columnlist) === 8);
  134. $this->assert($columnlist[0] === 'id');
  135. $this->assert($columnlist[5] === 'height');
  136. $tablelist = DB::tableList();
  137. $this->assert(count($tablelist) === 3);
  138. $this->assert($tablelist[0] === 'accounts');
  139. $tablelist = null;
  140. $tablelist = DB::tableList(DB::$dbName);
  141. $this->assert(count($tablelist) === 3);
  142. $this->assert($tablelist[0] === 'accounts');
  143. }
  144. function test_4_1_query() {
  145. DB::insert('accounts', array(
  146. 'username' => 'newguy',
  147. 'password' => DB::sqleval("REPEAT('blah', %i)", '3'),
  148. 'age' => DB::sqleval('171+1'),
  149. 'height' => 111.15
  150. ));
  151. $row = DB::queryOneRow("SELECT * FROM accounts WHERE password=%s", 'blahblahblah');
  152. $this->assert($row['username'] === 'newguy');
  153. $this->assert($row['age'] === '172');
  154. $true = DB::update('accounts', array(
  155. 'password' => DB::sqleval("REPEAT('blah', %i)", 4),
  156. 'favorite_word' => null,
  157. ), 'username=%s_name', array('name' => 'newguy'));
  158. $row = null;
  159. $row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy');
  160. $this->assert($true === true);
  161. $this->assert($row['password'] === 'blahblahblahblah');
  162. $this->assert($row['favorite_word'] === null);
  163. $row = DB::query("SELECT * FROM accounts WHERE password=%s_mypass AND (password=%s_mypass) AND username=%s_myuser",
  164. array('myuser' => 'newguy', 'mypass' => 'blahblahblahblah')
  165. );
  166. $this->assert(count($row) === 1);
  167. $this->assert($row[0]['username'] === 'newguy');
  168. $this->assert($row[0]['age'] === '172');
  169. $row = DB::query("SELECT * FROM accounts WHERE password IN %li AND password IN %li0 AND username=%s", array('blahblahblahblah'), 'newguy');
  170. $this->assert(count($row) === 1);
  171. $this->assert($row[0]['username'] === 'newguy');
  172. $this->assert($row[0]['age'] === '172');
  173. $true = DB::query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
  174. $this->assert($true === true);
  175. $this->assert(DB::affectedRows() === 1);
  176. }
  177. function test_4_2_delete() {
  178. DB::insert('accounts', array(
  179. 'username' => 'gonesoon',
  180. 'password' => 'something',
  181. 'age' => 61,
  182. 'height' => 199.194
  183. ));
  184. $ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE %ha", array('username' => 'gonesoon', 'height' => 199.194));
  185. $this->assert(intval($ct) === 1);
  186. $ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s1 AND height=%d0 AND height=%d", 199.194, 'gonesoon');
  187. $this->assert(intval($ct) === 1);
  188. DB::delete('accounts', 'username=%s AND age=%i AND height=%d', 'gonesoon', '61', '199.194');
  189. $this->assert(DB::affectedRows() === 1);
  190. $ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', '199.194');
  191. $this->assert(intval($ct) === 0);
  192. }
  193. function test_4_3_insertmany() {
  194. $ins[] = array(
  195. 'username' => '1ofmany',
  196. 'password' => 'something',
  197. 'age' => 23,
  198. 'height' => 190.194
  199. );
  200. $ins[] = array(
  201. 'password' => 'somethingelse',
  202. 'username' => '2ofmany',
  203. 'age' => 25,
  204. 'height' => 190.194
  205. );
  206. $ins[] = array(
  207. 'password' => NULL,
  208. 'username' => '3ofmany',
  209. 'age' => 15,
  210. 'height' => 111.951
  211. );
  212. DB::insert('accounts', $ins);
  213. $this->assert(DB::affectedRows() === 3);
  214. $rows = DB::query("SELECT * FROM accounts WHERE height=%d ORDER BY age ASC", 190.194);
  215. $this->assert(count($rows) === 2);
  216. $this->assert($rows[0]['username'] === '1ofmany');
  217. $this->assert($rows[0]['age'] === '23');
  218. $this->assert($rows[1]['age'] === '25');
  219. $this->assert($rows[1]['password'] === 'somethingelse');
  220. $this->assert($rows[1]['username'] === '2ofmany');
  221. $nullrow = DB::queryOneRow("SELECT * FROM accounts WHERE username LIKE %ss", '3ofman');
  222. $this->assert($nullrow['password'] === NULL);
  223. $this->assert($nullrow['age'] === '15');
  224. }
  225. function test_5_insert_blobs() {
  226. DB::query("CREATE TABLE `store data` (
  227. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  228. `picture` BLOB
  229. ) ENGINE = InnoDB");
  230. $columns = DB::columnList('store data');
  231. $this->assert(count($columns) === 2);
  232. $this->assert($columns[1] === 'picture');
  233. $smile = file_get_contents('smile1.jpg');
  234. DB::insert('store data', array(
  235. 'picture' => $smile,
  236. ));
  237. DB::queryOneRow("INSERT INTO %b (picture) VALUES (%s)", 'store data', $smile);
  238. $getsmile = DB::queryFirstField("SELECT picture FROM %b WHERE id=1", 'store data');
  239. $getsmile2 = DB::queryFirstField("SELECT picture FROM %b WHERE id=2", 'store data');
  240. $this->assert($smile === $getsmile);
  241. $this->assert($smile === $getsmile2);
  242. }
  243. function test_6_insert_ignore() {
  244. DB::insertIgnore('accounts', array(
  245. 'id' => 1, //duplicate primary key
  246. 'username' => 'gonesoon',
  247. 'password' => 'something',
  248. 'age' => 61,
  249. 'height' => 199.194
  250. ));
  251. }
  252. function test_7_insert_update() {
  253. $true = DB::insertUpdate('accounts', array(
  254. 'id' => 2, //duplicate primary key
  255. 'username' => 'gonesoon',
  256. 'password' => 'something',
  257. 'age' => 61,
  258. 'height' => 199.194
  259. ), 'age = age + %i', 1);
  260. $this->assert($true === true);
  261. $this->assert(DB::affectedRows() === 2); // a quirk of MySQL, even though only 1 row was updated
  262. $result = DB::query("SELECT * FROM accounts WHERE age = %i", 16);
  263. $this->assert(count($result) === 1);
  264. $this->assert($result[0]['height'] === '10.371');
  265. DB::insertUpdate('accounts', array(
  266. 'id' => 2, //duplicate primary key
  267. 'username' => 'blahblahdude',
  268. 'age' => 233,
  269. 'height' => 199.194
  270. ));
  271. $result = DB::query("SELECT * FROM accounts WHERE age = %i", 233);
  272. $this->assert(count($result) === 1);
  273. $this->assert($result[0]['height'] === '199.194');
  274. $this->assert($result[0]['username'] === 'blahblahdude');
  275. DB::insertUpdate('accounts', array(
  276. 'id' => 2, //duplicate primary key
  277. 'username' => 'gonesoon',
  278. 'password' => 'something',
  279. 'age' => 61,
  280. 'height' => 199.194
  281. ), array(
  282. 'age' => 74,
  283. ));
  284. $result = DB::query("SELECT * FROM accounts WHERE age = %i", 74);
  285. $this->assert(count($result) === 1);
  286. $this->assert($result[0]['height'] === '199.194');
  287. $this->assert($result[0]['username'] === 'blahblahdude');
  288. $multiples[] = array(
  289. 'id' => 3, //duplicate primary key
  290. 'username' => 'gonesoon',
  291. 'password' => 'something',
  292. 'age' => 61,
  293. 'height' => 199.194
  294. );
  295. $multiples[] = array(
  296. 'id' => 1, //duplicate primary key
  297. 'username' => 'gonesoon',
  298. 'password' => 'something',
  299. 'age' => 61,
  300. 'height' => 199.194
  301. );
  302. DB::insertUpdate('accounts', $multiples, array('age' => 914));
  303. $this->assert(DB::affectedRows() === 4);
  304. $result = DB::query("SELECT * FROM accounts WHERE age=914 ORDER BY id ASC");
  305. $this->assert(count($result) === 2);
  306. $this->assert($result[0]['username'] === 'Abe');
  307. $this->assert($result[1]['username'] === 'Charlie\'s Friend');
  308. $true = DB::query("UPDATE accounts SET age=15, username='Bart' WHERE age=%i", 74);
  309. $this->assert($true === true);
  310. $this->assert(DB::affectedRows() === 1);
  311. }
  312. function test_8_lb() {
  313. $data = array(
  314. 'username' => 'vookoo',
  315. 'password' => 'dookoo',
  316. );
  317. $true = DB::query("INSERT into accounts %lb VALUES %ls", array_keys($data), array_values($data));
  318. $result = DB::query("SELECT * FROM accounts WHERE username=%s", 'vookoo');
  319. $this->assert($true === true);
  320. $this->assert(count($result) === 1);
  321. $this->assert($result[0]['password'] === 'dookoo');
  322. }
  323. function test_9_fullcolumns() {
  324. $true = DB::insert('profile', array(
  325. 'id' => 1,
  326. 'signature' => 'u_suck'
  327. ));
  328. DB::query("UPDATE accounts SET profile_id=1 WHERE id=2");
  329. $r = DB::queryFullColumns("SELECT accounts.*, profile.*, 1+1 FROM accounts
  330. INNER JOIN profile ON accounts.profile_id=profile.id");
  331. $this->assert($true === true);
  332. $this->assert(count($r) === 1);
  333. $this->assert($r[0]['accounts.id'] === '2');
  334. $this->assert($r[0]['profile.id'] === '1');
  335. $this->assert($r[0]['profile.signature'] === 'u_suck');
  336. $this->assert($r[0]['1+1'] === '2');
  337. }
  338. function test_901_updatewithspecialchar() {
  339. $data = 'www.mysite.com/product?s=t-%s-%%3d%%3d%i&RCAID=24322';
  340. DB::update('profile', array('signature' => $data), 'id=%i', 1);
  341. $signature = DB::queryFirstField("SELECT signature FROM profile WHERE id=%i", 1);
  342. $this->assert($signature === $data);
  343. DB::update('profile',array('signature'=> "%li "),"id = %d",1);
  344. $signature = DB::queryFirstField("SELECT signature FROM profile WHERE id=%i", 1);
  345. $this->assert($signature === "%li ");
  346. }
  347. function test_902_faketable() {
  348. DB::insert('fake%s_table', array('name' => 'karen'));
  349. $count = DB::queryFirstField("SELECT COUNT(*) FROM %b", 'fake%s_table');
  350. $this->assert($count === '1');
  351. DB::update('fake%s_table', array('name' => 'haren%s'), 'name=%s_name', array('name' => 'karen'));
  352. DB::delete('fake%s_table', 'name=%s0', 'haren%s');
  353. $count = DB::queryFirstField("SELECT COUNT(*) FROM %b", 'fake%s_table');
  354. $this->assert($count === '0');
  355. }
  356. }
  357. ?>