PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/konstrukt/test/k.test.php

https://github.com/archives-of-michigan/ContentDM-REST
PHP | 410 lines | 385 code | 23 blank | 2 comment | 3 complexity | 771eaf43f57bf76369b130955138f219 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. error_reporting(E_ALL | E_STRICT);
  3. set_include_path(
  4. get_include_path().PATH_SEPARATOR.
  5. join(PATH_SEPARATOR, array(dirname(__FILE__).'/../../../test')).PATH_SEPARATOR.
  6. join(PATH_SEPARATOR, array(dirname(__FILE__).'/../lib'))
  7. );
  8. // You need to have simpletest in your include_path
  9. require_once 'simpletest/unit_tester.php';
  10. if (realpath($_SERVER['PHP_SELF']) == __FILE__) {
  11. require_once 'simpletest/autorun.php';
  12. }
  13. require_once '../lib/konstrukt/konstrukt.inc.php';
  14. require_once 'support/mocks.inc.php';
  15. class TestOfGlobalsAccess extends UnitTestCase {
  16. function setUp() {
  17. $this->GET = $_GET;
  18. $_GET = array();
  19. }
  20. function tearDown() {
  21. $_GET = $this->GET;
  22. }
  23. function test_undo_magic_quotes_if_present() {
  24. $g = new k_adapter_SafeGlobalsAccess(new k_charset_Latin1CharsetStrategy(), true);
  25. $_GET = array(
  26. 'name' => "O\\'Reilly"
  27. );
  28. $this->assertEqual($g->query(), array('name' => "O'Reilly"));
  29. }
  30. function test_doesnt_undo_magic_quotes_if_not_present() {
  31. $g = new k_adapter_SafeGlobalsAccess(new k_charset_Latin1CharsetStrategy(), false);
  32. $_GET = array(
  33. 'name' => "O\\'Reilly"
  34. );
  35. $this->assertEqual($g->query(), array('name' => "O\\'Reilly"));
  36. }
  37. function test_unmagic_on_deep_array_doesnt_crash_runtime() {
  38. $g = new k_adapter_SafeGlobalsAccess(new k_charset_Latin1CharsetStrategy(), true);
  39. eval("\$_GET = " . str_repeat("array(", 1024) . "\"O\\\\'Reilly\"" . str_repeat(")", 1024) . ";");
  40. $g->query(); // Note: A failing test will produce a Fatal Error and halt the suite
  41. }
  42. function test_mixed_case_key_doesnt_get_conflated() {
  43. $g = new k_adapter_SafeGlobalsAccess(new k_charset_Latin1CharsetStrategy(), true);
  44. $_GET = array(
  45. 'foo' => "42",
  46. 'Foo' => "1337"
  47. );
  48. $this->assertEqual($g->query(), array('foo' => "42", 'Foo' => "1337"));
  49. }
  50. }
  51. class TestOfFileUpload extends UnitTestCase {
  52. function test_files_array() {
  53. $files = array(
  54. 'userfile' => array(
  55. 'name' => 'test.pdf',
  56. 'tmp_name' => 'tmp85937457',
  57. 'size' => '1024',
  58. 'type' => 'application/pdf',
  59. 'error' => 0,
  60. )
  61. );
  62. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost', 'SCRIPT_NAME' => '', 'REQUEST_URI' => ''), array(), array(), $files);
  63. $http = new k_HttpRequest(null, null, new k_DefaultIdentityLoader(), $glob);
  64. $files = $http->file();
  65. $this->assertTrue(is_array($files));
  66. $file = $http->file('userfile');
  67. $this->assertEqual($file->name(), 'test.pdf');
  68. $this->assertEqual($file->type(), 'application/pdf');
  69. $this->assertEqual($file->size(), '1024');
  70. $this->assertEqual($file->key(), 'userfile');
  71. }
  72. function test_normalizing_files_array_when_multiple_files() {
  73. $files = array(
  74. 'userfile' => array(
  75. 'name' => array(
  76. 'test.pdf',
  77. 'test.doc',
  78. ),
  79. 'tmp_name' => array(
  80. 'tmp85937457',
  81. 'tmp45937457',
  82. ),
  83. 'size' => array(
  84. '1024',
  85. '2048',
  86. ),
  87. 'type' => array(
  88. 'application/pdf',
  89. 'application/ms-word',
  90. ),
  91. 'error' => array(
  92. 0,
  93. 0,
  94. )
  95. )
  96. );
  97. $expected = array(
  98. 'userfile' => array(
  99. array(
  100. 'name' => 'test.pdf',
  101. 'tmp_name' => 'tmp85937457',
  102. 'size' => '1024',
  103. 'type' => 'application/pdf',
  104. 'error' => 0,
  105. ),
  106. array(
  107. 'name' => 'test.doc',
  108. 'tmp_name' => 'tmp45937457',
  109. 'size' => '2048',
  110. 'type' => 'application/ms-word',
  111. 'error' => 0,
  112. )
  113. )
  114. );
  115. $g = new k_adapter_SafeGlobalsAccess(new k_charset_Latin1CharsetStrategy(), true);
  116. $normalized = $g->normalizeFiles($files);
  117. $this->assertEqual($expected, $normalized);
  118. }
  119. function test_saving_an_uploaded_file() {
  120. $files = array(
  121. 'userfile' => array(
  122. 'name' => 'test.pdf',
  123. 'tmp_name' => 'tmp85937457',
  124. 'size' => '1024',
  125. 'type' => 'application/pdf',
  126. 'error' => 0,
  127. )
  128. );
  129. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost', 'SCRIPT_NAME' => '', 'REQUEST_URI' => ''), array(), array(), $files);
  130. $file_access = new k_adapter_MockUploadedFileAccess();
  131. $http = new k_HttpRequest(null, null, new k_DefaultIdentityLoader(), $glob, null, null, $file_access);
  132. $http->file('userfile')->writeTo('/dev/null');
  133. $this->assertEqual(array(array('tmp85937457', '/dev/null')), $file_access->actions);
  134. }
  135. }
  136. class TestOfDispatching extends UnitTestCase {
  137. function test_root_gives_request_uri_as_subspace() {
  138. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost'));
  139. $http = new k_HttpRequest('', '/foo/bar', new k_DefaultIdentityLoader(), $glob);
  140. $this->assertEqual($http->subspace(), "/foo/bar");
  141. }
  142. function test_first_component_has_root_subspace() {
  143. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost'));
  144. $http = new k_HttpRequest('', '/foo/bar', new k_DefaultIdentityLoader(), $glob);
  145. $components = new k_DefaultComponentCreator();
  146. $root = $components->create('test_CircularComponent', $http);
  147. $this->assertEqual($root->subspace(), "foo/bar");
  148. }
  149. function test_dispatch() {
  150. $expected = "Dispatching:
  151. name: ''
  152. next: 'foo'
  153. subtype: ''
  154. url: '/web2.0/'
  155. Dispatching:
  156. name: 'foo'
  157. next: 'bar'
  158. subtype: ''
  159. url: '/web2.0/foo'
  160. Dispatching:
  161. name: 'bar'
  162. next: 'cux'
  163. subtype: ''
  164. url: '/web2.0/foo/bar'
  165. Dispatching:
  166. name: 'cux'
  167. next: ''
  168. subtype: ''
  169. url: '/web2.0/foo/bar/cux'
  170. Executing
  171. ";
  172. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost'));
  173. $http = new k_HttpRequest('/web2.0', '/web2.0/foo/bar/cux', new k_DefaultIdentityLoader(), $glob);
  174. $components = new k_DefaultComponentCreator();
  175. $root = $components->create('test_CircularComponent', $http);
  176. $result = $root->dispatch();
  177. $this->assertEqual($result, $expected);
  178. $http = new k_HttpRequest('/web2.0', '/web2.0/foo/bar/cux/', new k_DefaultIdentityLoader(), $glob);
  179. $components = new k_DefaultComponentCreator();
  180. $root = $components->create('test_CircularComponent', $http);
  181. $result = $root->dispatch();
  182. $this->assertEqual($result, $expected);
  183. }
  184. function test_subtype_doesnt_affect_dispatch() {
  185. $expected = "Dispatching:
  186. name: ''
  187. next: 'foo'
  188. subtype: ''
  189. url: '/web2.0/'
  190. Dispatching:
  191. name: 'foo'
  192. next: 'bar'
  193. subtype: 'ninja'
  194. url: '/web2.0/foo;ninja'
  195. Dispatching:
  196. name: 'bar'
  197. next: ''
  198. subtype: ''
  199. url: '/web2.0/foo;ninja/bar'
  200. Executing
  201. ";
  202. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost'));
  203. $http = new k_HttpRequest('/web2.0', '/foo;ninja/bar', new k_DefaultIdentityLoader(), $glob);
  204. $components = new k_DefaultComponentCreator();
  205. $root = $components->create('test_CircularComponent', $http);
  206. $result = $root->dispatch();
  207. $this->assertEqual($result, $expected);
  208. }
  209. function test_using_front_controller() {
  210. $expected = "Dispatching:
  211. name: ''
  212. next: 'foo'
  213. subtype: ''
  214. url: '/web2.0/'
  215. Dispatching:
  216. name: 'foo'
  217. next: 'bar'
  218. subtype: 'ninja'
  219. url: '/web2.0/foo;ninja'
  220. Dispatching:
  221. name: 'bar'
  222. next: ''
  223. subtype: ''
  224. url: '/web2.0/foo;ninja/bar'
  225. Executing
  226. ";
  227. $glob = new k_adapter_MockGlobalsAccess(array('q' => '/foo;ninja/bar'), array(),
  228. array('SERVER_NAME' => 'localhost', 'SCRIPT_NAME' => '/index.php'));
  229. $http = new k_HttpRequest('/web2.0', '/index.php', new k_DefaultIdentityLoader(), $glob, null, null, null, true);
  230. $components = new k_DefaultComponentCreator();
  231. $root = $components->create('test_CircularComponent', $http);
  232. $result = $root->dispatch();
  233. $this->assertEqual($result, $expected);
  234. }
  235. }
  236. class TestOfUrlGeneration extends UnitTestCase {
  237. function createHttp($href_base, $request_uri) {
  238. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'example.org'));
  239. return new k_HttpRequest($href_base, $request_uri, new k_DefaultIdentityLoader(), $glob);
  240. }
  241. function test_url_state_param_propagates_over_url() {
  242. $http = $this->createHttp('', '/foo/bar');
  243. $components = new k_DefaultComponentCreator();
  244. $root = $components->create('test_ExposedComponent', $http);
  245. $root->getUrlState()->set('foo', 'bar');
  246. $this->assertEqual($root->url('', array('zip' => 'zap')), "/?foo=bar&zip=zap");
  247. }
  248. function test_getting_unset_stateful_parameter_defaults_to_null() {
  249. $http = $this->createHttp('', '/foo/bar');
  250. $components = new k_DefaultComponentCreator();
  251. $root = $components->create('test_ExposedComponent', $http);
  252. $this->assertNull($root->getUrlState()->get('foo'));
  253. }
  254. function test_url_state_param_set_to_default_value_doesnt_propagate() {
  255. $http = $this->createHttp('', '/foo/bar');
  256. $components = new k_DefaultComponentCreator();
  257. $root = $components->create('test_ExposedComponent', $http);
  258. $root->getUrlState()->init('foo', 'bar');
  259. $root->getUrlState()->set('foo', 'bar');
  260. $this->assertEqual($root->url('', array('zip' => 'zap')), "/?zip=zap");
  261. }
  262. function test_create_href() {
  263. $http = $this->createHttp('', '/foo/bar');
  264. $components = new k_DefaultComponentCreator();
  265. $root = $components->create('test_CircularComponent', $http);
  266. $first = $components->create('test_CircularComponent', $root);
  267. $second = $components->create('test_CircularComponent', $first);
  268. $this->assertEqual($second->url(), "/foo/bar");
  269. $this->assertEqual($second->url(".."), "/foo");
  270. $this->assertEqual($second->url("/ding"), "/ding");
  271. }
  272. function test_root_must_end_with_trailing_slash_but_subcomponents_mustnt() {
  273. /*
  274. * This probably deserves some explanation.
  275. * Basically, there are two schools of thought, in regards to URI paths
  276. * One sees paths as filesystem paths, and it therefore makes sense to display
  277. * directories with trailing slash, but files without.
  278. * The other perspective is that paths could address any kind of hierarchical
  279. * structure, where there isn't any distinction between node types. In such a
  280. * system, a node should never be addressed with a trailing slash - Except
  281. * for the root-node.
  282. */
  283. $http = $this->createHttp('/foo', '/foo/bar');
  284. $components = new k_DefaultComponentCreator();
  285. $root = $components->create('test_CircularComponent', $http);
  286. $first = $components->create('test_CircularComponent', $root);
  287. $this->assertEqual($root->url(), "/foo/");
  288. $this->assertEqual($first->url(), "/foo/bar");
  289. }
  290. // Bug reported by Ander Ekdahl
  291. function test_when_script_name_is_slash_href_base_should_be_slash() {
  292. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'example.org', 'SCRIPT_NAME' => '/', 'REQUEST_URI' => '/'));
  293. $http = new k_HttpRequest(null, null, new k_DefaultIdentityLoader(), $glob);
  294. $this->assertEqual('/', $http->url());
  295. }
  296. function test_when_specifying_a_subtype_it_replaces_the_current() {
  297. $http = $this->createHttp('/foo', '/foo/bar;text');
  298. $components = new k_DefaultComponentCreator();
  299. $root = $components->create('test_CircularComponent', $http);
  300. $first = $components->create('test_CircularComponent', $root);
  301. $this->assertEqual($first->url(";xml"), "/foo/bar;xml");
  302. }
  303. function test_an_subtype_is_removed() {
  304. $http = $this->createHttp('/foo', '/foo/bar;text');
  305. $components = new k_DefaultComponentCreator();
  306. $root = $components->create('test_CircularComponent', $http);
  307. $first = $components->create('test_CircularComponent', $root);
  308. $this->assertEqual($first->url(";"), "/foo/bar");
  309. }
  310. function test_passing_array_as_first_argument_joins_segments() {
  311. $http = $this->createHttp('', '');
  312. $components = new k_DefaultComponentCreator();
  313. $this->assertEqual($http->url(array('foo', 'bar')), "foo/bar");
  314. }
  315. function test_passing_array_as_first_argument_encodes_segments() {
  316. $http = $this->createHttp('', '');
  317. $components = new k_DefaultComponentCreator();
  318. $this->assertEqual($http->url(array("bl\xC3\xA5b\xC3\xA6rgr\xC3\xB8d", 'bar')), "bl%C3%A5b%C3%A6rgr%C3%B8d/bar");
  319. }
  320. function test_passing_array_as_first_argument_doesnt_encode_semicolons() {
  321. $http = $this->createHttp('', '');
  322. $components = new k_DefaultComponentCreator();
  323. $this->assertEqual($http->url(array("banjo;html")), "banjo;html");
  324. }
  325. }
  326. class TestOfHttpRequest extends UnitTestCase {
  327. function createHttp($headers) {
  328. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'example.org'), $headers);
  329. return new k_HttpRequest('', '', new k_DefaultIdentityLoader(), $glob);
  330. }
  331. function test_negotiate_returns_first_match_from_accept_header() {
  332. $request = $this->createHttp(array('accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  333. $this->assertEqual($request->negotiateContentType(array('text/html','application/xhtml+xml','application/xml')), 'text/html');
  334. }
  335. function test_negotiate_returns_match_with_highest_rank_from_accept_header() {
  336. $request = $this->createHttp(array('accept' => 'text/html;q=0.1,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  337. $this->assertEqual($request->negotiateContentType(array('text/html','application/xhtml+xml','application/xml')), 'application/xhtml+xml');
  338. }
  339. function test_negotiate_returns_candidate_with_exact_match() {
  340. $request = $this->createHttp(array('accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  341. $this->assertEqual($request->negotiateContentType(array('application/xml')), 'application/xml');
  342. }
  343. function test_negotiate_returns_candidate_with_partial_match() {
  344. $request = $this->createHttp(array('accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  345. $this->assertEqual($request->negotiateContentType(array('application/*')), 'application/*');
  346. }
  347. function test_negotiate_returns_first_candidate_on_no_header() {
  348. $request = $this->createHttp(array('accept' => ''));
  349. $this->assertEqual($request->negotiateContentType(array('text/html')), 'text/html');
  350. }
  351. function test_user_override_overrides_accept_header() {
  352. $request = $this->createHttp(array('accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  353. $this->assertEqual($request->negotiateContentType(array('text/html', 'foo/bar'), 'foo/bar'), 'foo/bar');
  354. }
  355. function test_negotiate_simple_subtype_interpreted_as_major_type() {
  356. $request = $this->createHttp(array('accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  357. $this->assertEqual($request->negotiateContentType(array('text/html', 'foo'), 'foo'), 'foo');
  358. }
  359. function test_negotiate_internet_explorer_headerline() {
  360. $request = $this->createHttp(array('accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*'));
  361. $this->assertEqual($request->negotiateContentType(array('text/html')), 'text/html');
  362. }
  363. }
  364. class TestOfHttpResponse extends UnitTestCase {
  365. function test_404_response_should_output_custom_content_if_any() {
  366. $output = new k_adapter_DummyOutputAccess();
  367. $response = new k_HttpResponse(404, "I didn't find it");
  368. $response->out($output);
  369. $this->assertEqual(404, $output->http_response_code);
  370. $this->assertEqual("I didn't find it", $output->body);
  371. }
  372. }