PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/test/k.test.php

https://github.com/RussellDias/konstrukt
PHP | 352 lines | 328 code | 3 blank | 21 comment | 2 complexity | f8db825950c6e2e800dcea720237d038 MD5 | raw file
  1. <?php
  2. error_reporting(E_ALL | E_STRICT);
  3. set_include_path(__DIR__ . PATH_SEPARATOR . __DIR__ . '/../lib/' . PATH_SEPARATOR . get_include_path());
  4. // You need to have simpletest in your include_path
  5. require_once 'simpletest/unit_tester.php';
  6. if (realpath($_SERVER['SCRIPT_FILENAME']) == __FILE__) {
  7. require_once 'simpletest/autorun.php';
  8. }
  9. require_once '../lib/konstrukt/konstrukt.inc.php';
  10. require_once 'support/mocks.inc.php';
  11. class TestOfGlobalsAccess extends UnitTestCase {
  12. function setUp() {
  13. $this->GET = $_GET;
  14. $_GET = array();
  15. }
  16. function tearDown() {
  17. $_GET = $this->GET;
  18. }
  19. function test_undo_magic_quotes_if_present() {
  20. $g = new k_adapter_SafeGlobalsAccess(new k_charset_Latin1CharsetStrategy(), true);
  21. $_GET = array(
  22. 'name' => "O\\'Reilly"
  23. );
  24. $this->assertEqual($g->query(), array('name' => "O'Reilly"));
  25. }
  26. function test_doesnt_undo_magic_quotes_if_not_present() {
  27. $g = new k_adapter_SafeGlobalsAccess(new k_charset_Latin1CharsetStrategy(), false);
  28. $_GET = array(
  29. 'name' => "O\\'Reilly"
  30. );
  31. $this->assertEqual($g->query(), array('name' => "O\\'Reilly"));
  32. }
  33. function test_unmagic_on_deep_array_doesnt_crash_runtime() {
  34. $g = new k_adapter_SafeGlobalsAccess(new k_charset_Latin1CharsetStrategy(), true);
  35. eval("\$_GET = " . str_repeat("array(", 1024) . "\"O\\\\'Reilly\"" . str_repeat(")", 1024) . ";");
  36. $g->query(); // Note: A failing test will produce a Fatal Error and halt the suite
  37. }
  38. function test_mixed_case_key_doesnt_get_conflated() {
  39. $g = new k_adapter_SafeGlobalsAccess(new k_charset_Latin1CharsetStrategy(), true);
  40. $_GET = array(
  41. 'foo' => "42",
  42. 'Foo' => "1337"
  43. );
  44. $this->assertEqual($g->query(), array('foo' => "42", 'Foo' => "1337"));
  45. }
  46. }
  47. class TestOfFileUpload extends UnitTestCase {
  48. function test_files_array() {
  49. $files = array(
  50. 'userfile' => array(
  51. 'name' => 'test.pdf',
  52. 'tmp_name' => 'tmp85937457',
  53. 'size' => '1024',
  54. 'type' => 'application/pdf',
  55. 'error' => 0,
  56. )
  57. );
  58. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost', 'SCRIPT_NAME' => '', 'REQUEST_URI' => ''), array(), array(), $files);
  59. $http = new k_HttpRequest(null, null, new k_DefaultIdentityLoader(), null, null, $glob);
  60. $files = $http->file();
  61. $this->assertTrue(is_array($files));
  62. $file = $http->file('userfile');
  63. $this->assertEqual($file->name(), 'test.pdf');
  64. $this->assertEqual($file->type(), 'application/pdf');
  65. $this->assertEqual($file->size(), '1024');
  66. $this->assertEqual($file->key(), 'userfile');
  67. }
  68. function test_normalizing_files_array_when_multiple_files() {
  69. $files = array(
  70. 'userfile' => array(
  71. 'name' => array(
  72. 'test.pdf',
  73. 'test.doc',
  74. ),
  75. 'tmp_name' => array(
  76. 'tmp85937457',
  77. 'tmp45937457',
  78. ),
  79. 'size' => array(
  80. '1024',
  81. '2048',
  82. ),
  83. 'type' => array(
  84. 'application/pdf',
  85. 'application/ms-word',
  86. ),
  87. 'error' => array(
  88. 0,
  89. 0,
  90. )
  91. )
  92. );
  93. $expected = array(
  94. 'userfile' => array(
  95. array(
  96. 'name' => 'test.pdf',
  97. 'tmp_name' => 'tmp85937457',
  98. 'size' => '1024',
  99. 'type' => 'application/pdf',
  100. 'error' => 0,
  101. ),
  102. array(
  103. 'name' => 'test.doc',
  104. 'tmp_name' => 'tmp45937457',
  105. 'size' => '2048',
  106. 'type' => 'application/ms-word',
  107. 'error' => 0,
  108. )
  109. )
  110. );
  111. $g = new k_adapter_SafeGlobalsAccess(new k_charset_Latin1CharsetStrategy(), true);
  112. $normalized = $g->normalizeFiles($files);
  113. $this->assertEqual($expected, $normalized);
  114. }
  115. function test_saving_an_uploaded_file() {
  116. $files = array(
  117. 'userfile' => array(
  118. 'name' => 'test.pdf',
  119. 'tmp_name' => 'tmp85937457',
  120. 'size' => '1024',
  121. 'type' => 'application/pdf',
  122. 'error' => 0,
  123. )
  124. );
  125. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost', 'SCRIPT_NAME' => '', 'REQUEST_URI' => ''), array(), array(), $files);
  126. $file_access = new k_adapter_MockUploadedFileAccess();
  127. $http = new k_HttpRequest(null, null, new k_DefaultIdentityLoader(), null, null, $glob, null, null, $file_access);
  128. $http->file('userfile')->writeTo('/dev/null');
  129. $this->assertEqual(array(array('tmp85937457', '/dev/null')), $file_access->actions);
  130. }
  131. }
  132. class TestOfDispatching extends UnitTestCase {
  133. function test_root_gives_request_uri_as_subspace() {
  134. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost'));
  135. $http = new k_HttpRequest('', '/foo/bar', new k_DefaultIdentityLoader(), null, null, $glob);
  136. $this->assertEqual($http->subspace(), "/foo/bar");
  137. }
  138. function test_first_component_has_root_subspace() {
  139. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost'));
  140. $http = new k_HttpRequest('', '/foo/bar', new k_DefaultIdentityLoader(), null, null, $glob);
  141. $components = new k_DefaultComponentCreator();
  142. $root = $components->create('test_CircularComponent', $http);
  143. $this->assertEqual($root->subspace(), "foo/bar");
  144. }
  145. function test_dispatch() {
  146. $expected = "Dispatching:
  147. name: ''
  148. next: 'foo'
  149. subtype: ''
  150. url: '/web2.0/'
  151. Dispatching:
  152. name: 'foo'
  153. next: 'bar'
  154. subtype: ''
  155. url: '/web2.0/foo'
  156. Dispatching:
  157. name: 'bar'utorial
  158. next: 'cux'
  159. subtype: ''
  160. url: '/web2.0/foo/bar'
  161. Dispatching:
  162. name: 'cux'
  163. next: ''
  164. subtype: ''
  165. url: '/web2.0/foo/bar/cux'
  166. Executing
  167. ";
  168. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost'));
  169. $http = new k_HttpRequest('/web2.0', '/web2.0/foo/bar/cux', new k_DefaultIdentityLoader(), null, null, $glob);
  170. $components = new k_DefaultComponentCreator();
  171. $root = $components->create('test_CircularComponent', $http);
  172. $result = $root->dispatch();
  173. $this->assertEqual($result, $expected);
  174. $http = new k_HttpRequest('/web2.0', '/web2.0/foo/bar/cux/', new k_DefaultIdentityLoader(), null, null, $glob);
  175. $components = new k_DefaultComponentCreator();
  176. $root = $components->create('test_CircularComponent', $http);
  177. $result = $root->dispatch();
  178. $this->assertEqual($result, $expected);
  179. }
  180. function test_subtype_doesnt_affect_dispatch() {
  181. $expected = "Dispatching:
  182. name: ''
  183. next: 'foo'
  184. subtype: ''
  185. url: '/web2.0/'
  186. Dispatching:
  187. name: 'foo'
  188. next: 'bar'
  189. subtype: 'ninja'
  190. url: '/web2.0/foo.ninja'
  191. Dispatching:
  192. name: 'bar'
  193. next: ''
  194. subtype: ''
  195. url: '/web2.0/foo.ninja/bar'
  196. Executing
  197. ";
  198. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost'));
  199. $http = new k_HttpRequest('/web2.0', '/foo.ninja/bar', new k_DefaultIdentityLoader(), null, null, $glob);
  200. $components = new k_DefaultComponentCreator();
  201. $root = $components->create('test_CircularComponent', $http);
  202. $result = $root->dispatch();
  203. $this->assertEqual($expected, $result);
  204. }
  205. }
  206. class TestOfHttpRequest extends UnitTestCase {
  207. function createHttp($headers) {
  208. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'example.org'), $headers);
  209. return new k_HttpRequest('', '', new k_DefaultIdentityLoader(), null, null, $glob);
  210. }
  211. function test_negotiate_returns_first_match_from_accept_header() {
  212. $request = $this->createHttp(array('accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  213. $this->assertEqual($request->negotiateContentType(array('text/html','application/xhtml+xml','application/xml')), 'text/html');
  214. }
  215. function test_negotiate_returns_match_with_highest_rank_from_accept_header() {
  216. $request = $this->createHttp(array('accept' => 'text/html;q=0.1,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  217. $this->assertEqual($request->negotiateContentType(array('text/html','application/xhtml+xml','application/xml')), 'application/xhtml+xml');
  218. }
  219. function test_negotiate_returns_candidate_with_exact_match() {
  220. $request = $this->createHttp(array('accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  221. $this->assertEqual($request->negotiateContentType(array('application/xml')), 'application/xml');
  222. }
  223. function test_negotiate_returns_candidate_with_partial_match() {
  224. $request = $this->createHttp(array('accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  225. $this->assertEqual($request->negotiateContentType(array('application/*')), 'application/*');
  226. }
  227. function test_negotiate_returns_first_candidate_on_no_header() {
  228. $request = $this->createHttp(array('accept' => ''));
  229. $this->assertEqual($request->negotiateContentType(array('text/html')), 'text/html');
  230. }
  231. function test_user_override_overrides_accept_header() {
  232. $request = $this->createHttp(array('accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  233. $this->assertEqual($request->negotiateContentType(array('text/html', 'foo/bar'), 'foo/bar'), 'foo/bar');
  234. }
  235. function test_negotiate_simple_subtype_interpreted_as_major_type() {
  236. $request = $this->createHttp(array('accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
  237. $this->assertEqual($request->negotiateContentType(array('text/html', 'foo'), 'foo'), 'foo');
  238. }
  239. function test_negotiate_internet_explorer_headerline() {
  240. $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, */*'));
  241. $this->assertEqual($request->negotiateContentType(array('text/html')), 'text/html');
  242. }
  243. }
  244. class TestOfHttpResponse extends UnitTestCase {
  245. function test_404_response_should_output_custom_content_if_any() {
  246. $output = new k_adapter_DummyOutputAccess();
  247. $response = new k_HtmlResponse("I didn't find it");
  248. $response->setStatus(404);
  249. $response->out($output);
  250. $this->assertEqual(404, $output->http_response_code);
  251. $this->assertEqual("I didn't find it", $output->body);
  252. }
  253. }
  254. class TestOfContentTypeDispathing extends UnitTestCase {
  255. function createComponent($method, $headers = array()) {
  256. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost', 'REQUEST_METHOD' => $method), $headers);
  257. $http = new k_HttpRequest('', '/', new k_DefaultIdentityLoader(), null, null, $glob);
  258. $components = new k_DefaultComponentCreator();
  259. return $components->create('test_ContentTypeComponent', $http);
  260. }
  261. function test_posting_with_a_content_type_calls_specific_handler() {
  262. $root = $this->createComponent('post', array('content-type' => 'application/json'));
  263. $this->assertEqual("postJson called", $root->dispatch());
  264. }
  265. function test_post_multipart() {
  266. $root = $this->createComponent('post', array('content-type' => 'multipart/form-data; boundary=---------------------------1991290281749441721928095653'));
  267. $this->assertEqual("postMultipart called", $root->dispatch());
  268. }
  269. function test_posting_without_a_content_type_fails_with_not_acceptable_when_there_is_at_least_one_candidate() {
  270. $root = $this->createComponent('post');
  271. try {
  272. $root->dispatch();
  273. $this->fail("Expected exception not caught");
  274. } catch (k_NotAcceptable $ex) {
  275. $this->pass();
  276. }
  277. }
  278. function test_putting_without_a_content_type_fails_with_not_implemented_when_there_are_no_candidates() {
  279. $root = $this->createComponent('put');
  280. try {
  281. $root->dispatch();
  282. $this->fail("Expected exception not caught");
  283. } catch (k_NotImplemented $ex) {
  284. $this->pass();
  285. }
  286. }
  287. function test_a_content_type_with_explicit_charset_utf8_is_treated_transparently() {
  288. $root = $this->createComponent('post', array('content-type' => 'application/json; charset=utf-8'));
  289. $this->assertEqual("postJson called", $root->dispatch());
  290. }
  291. function test_a_content_type_with_explicit_charset_other_than_utf8_raises_an_error() {
  292. $root = $this->createComponent('post', array('content-type' => 'application/json; charset=iso-8859-1'));
  293. try {
  294. $root->dispatch();
  295. $this->fail("Expected exception");
  296. } catch (k_UnsupportedContentTypeCharsetException $ex) {
  297. $this->pass("Exception caught");
  298. }
  299. }
  300. }
  301. class TestOfLanguageLoading {
  302. function createComponent($method, $headers = array()) {
  303. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost', 'REQUEST_METHOD' => $method), $headers);
  304. $language_loader = new test_LanguageLoader();
  305. $http = new k_HttpRequest('', '/', new k_DefaultIdentityLoader(), $language_loader, null, $glob);
  306. $components = new k_DefaultComponentCreator();
  307. return $components->create('test_ContentTypeComponent', $http);
  308. }
  309. function test_that_a_language_is_loaded() {
  310. $component = $this->createComponent('get');
  311. $language = $component->language();
  312. $this->assertTrue($language instanceof k_Language);
  313. }
  314. }
  315. class TestOfTranslatorLoading {
  316. function createComponent($method, $headers = array()) {
  317. $glob = new k_adapter_MockGlobalsAccess(array(), array(), array('SERVER_NAME' => 'localhost', 'REQUEST_METHOD' => $method), $headers);
  318. $translator_loader = new test_TranslatorLoader();
  319. $http = new k_HttpRequest('', '/', new k_DefaultIdentityLoader(), null, $translator_loader, $glob);
  320. $components = new k_DefaultComponentCreator();
  321. return $components->create('test_ContentTypeComponent', $http);
  322. }
  323. function test_that_a_translator_is_loaded() {
  324. $component = $this->createComponent('get');
  325. $translator = $component->language();
  326. $this->assertTrue($translator instanceof k_Translator);
  327. }
  328. }