PageRenderTime 131ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Molinos/Base/Tests.php

https://code.google.com/p/molinos-cms/
PHP | 519 lines | 382 code | 81 blank | 56 comment | 2 complexity | 2fb977a3ed3c65cafec2474ed45327d3 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. class Molinos_Base_Tests extends Molinos_Testing_Case
  3. {
  4. /**
  5. * @expectedException InvalidArgumentException
  6. */
  7. public function testHtmlEmNoArgs()
  8. {
  9. Molinos_Base_XML::em();
  10. }
  11. /**
  12. * @expectedException InvalidArgumentException
  13. */
  14. public function testHtmlEmTooManyArgs()
  15. {
  16. Molinos_Base_XML::em(1, 2, 3, 4);
  17. }
  18. /**
  19. * @expectedException InvalidArgumentException
  20. */
  21. public function testHtmlEmEmptyName()
  22. {
  23. Molinos_Base_XML::em(null);
  24. }
  25. public function testHtmlEmSimple()
  26. {
  27. $html = Molinos_Base_XML::em('br');
  28. $this->assertEquals('<br/>', $html);
  29. }
  30. public function testHtmlEmWithContent()
  31. {
  32. $html = Molinos_Base_XML::em('p', 'hello');
  33. $this->assertEquals('<p>hello</p>', $html);
  34. }
  35. public function testHtmlEmWithAttributes()
  36. {
  37. $html = Molinos_Base_XML::em('br', array(
  38. 'class' => 'hidden',
  39. ));
  40. $this->assertEquals('<br class=\'hidden\'/>', $html);
  41. }
  42. public function testHtmlEmTdThEmpty()
  43. {
  44. $html = Molinos_Base_XML::em('td');
  45. $this->assertEquals('<td>&nbsp;</td>', $html);
  46. $html = Molinos_Base_XML::em('th');
  47. $this->assertEquals('<th>&nbsp;</th>', $html);
  48. }
  49. public function testHtmlEmptyDiv()
  50. {
  51. $html = Molinos_Base_XML::em('div');
  52. $this->assertEquals('<div></div>', $html);
  53. $html = Molinos_Base_XML::em('script');
  54. $this->assertEquals('<script></script>', $html);
  55. $html = Molinos_Base_XML::em('textarea');
  56. $this->assertEquals('<textarea></textarea>', $html);
  57. $html = Molinos_Base_XML::em('span');
  58. $this->assertEquals('<span></span>', $html);
  59. $html = Molinos_Base_XML::em('base');
  60. $this->assertEquals('<base></base>', $html);
  61. $html = Molinos_Base_XML::em('a');
  62. $this->assertEquals('<a></a>', $html);
  63. }
  64. public function testHtmlAttrs()
  65. {
  66. $a = Molinos_Base_XML::attrs(array(
  67. 'class' => 'test',
  68. 'title' => 'hello',
  69. 'width' => 0,
  70. 'checked' => true,
  71. ));
  72. $this->assertEquals(" checked='yes' class='test' title='hello'", $a);
  73. }
  74. public function testHtmlNonUniqueClasses()
  75. {
  76. $a = Molinos_Base_XML::attrs(array(
  77. 'class' => 'test ok test ok xyz',
  78. ));
  79. $this->assertEquals(" class='test ok xyz'", $a);
  80. }
  81. public function testHtmlSimpleList()
  82. {
  83. $html = Molinos_Base_XML::simpleList(array('one', 'two'));
  84. $this->assertEquals('<li>one</li><li>two</li>', $html);
  85. }
  86. public function testHtmlSimpleOptions()
  87. {
  88. $html = Molinos_Base_XML::simpleOptions(array(
  89. 'a' => 'first',
  90. 'b' => 'second',
  91. ));
  92. $this->assertEquals("<option value='a'>first</option><option value='b'>second</option>", $html);
  93. }
  94. public function testHtmlCdata()
  95. {
  96. $this->assertEquals(null, Molinos_Base_XML::cdata(null));
  97. $this->assertEquals(null, Molinos_Base_XML::cdata(0));
  98. $this->assertEquals(null, Molinos_Base_XML::cdata(''));
  99. $this->assertEquals(null, Molinos_Base_XML::cdata(array(123)));
  100. $this->assertEquals(null, Molinos_Base_XML::cdata(new stdClass()));
  101. $this->assertEquals('test', Molinos_Base_XML::cdata('test'));
  102. $this->assertEquals('<![CDATA[&nbsp;]]>', Molinos_Base_XML::cdata('&nbsp;'));
  103. }
  104. public function formatExtras()
  105. {
  106. $html = Molinos_Base_XML::formatExtras(array(
  107. array(
  108. 'style',
  109. 'test.css'
  110. ),
  111. array(
  112. 'script',
  113. 'test.js',
  114. ),
  115. ));
  116. $this->assertEquals("<link rel='stylesheet' type='text/css' href='test.css' /><script type='text/javascript' src='test.js'></script>", $html);
  117. }
  118. public function testHtmlAttributeName()
  119. {
  120. $html = Molinos_Base_XML::em('test', array(
  121. '1' => 2,
  122. '_destination' => 1,
  123. '-destination' => 2,
  124. ));
  125. $this->assertEquals('<test/>', $html);
  126. }
  127. private $db;
  128. public function testNodeConnect()
  129. {
  130. $this->db = Molinos_Database_Connection::getInstance();
  131. $this->assertNotNull($this->db);
  132. }
  133. public function testNodeCreateNewNode()
  134. {
  135. $ctx = $this->ctx;
  136. $t = new Molinos_Database_Transaction();
  137. $node = Molinos_Core_API::getInstance('node')->create(array(
  138. 'class' => 'xyz',
  139. 'name' => 'hello',
  140. ));
  141. $this->assertTrue($node instanceof Molinos_Base_Node);
  142. $this->assertEquals('hello', $node->name);
  143. $this->assertNull($node->id);
  144. }
  145. public function testNodeSetNodeProperty()
  146. {
  147. $node = Molinos_Core_API::getInstance("node")->create(array(
  148. "class" => "xyz",
  149. ));
  150. $text = 'hello, world.';
  151. $node->name = $text;
  152. $this->assertEquals($text, $node->name);
  153. }
  154. public function testNodeIsset()
  155. {
  156. $node = Molinos_Core_API::getInstance("node")->create(array(
  157. "class" => "xyz",
  158. ));
  159. $this->assertFalse(isset($node->name));
  160. $node->name = 'hello';
  161. $this->assertTrue(isset($node->name));
  162. }
  163. public function testNodeCountNodes()
  164. {
  165. if ($this->db) {
  166. $count = $this->db->getResult("SELECT COUNT(*) FROM {node} WHERE `class` = 'type' AND `deleted` = 0");
  167. $this->assertEquals($count, Molinos_Core_API::getInstance('node')->count(array(
  168. 'class' => 'type',
  169. )));
  170. }
  171. }
  172. public function testNodeCountDeletedNodes()
  173. {
  174. if ($this->db) {
  175. $count = $this->db->getResult("SELECT COUNT(*) FROM {node} WHERE `class` = 'type' AND `deleted` = 1");
  176. $this->assertEquals($count, Molinos_Core_API::getInstance('node')->count(array(
  177. 'class' => 'type',
  178. 'deleted' => 1,
  179. )));
  180. }
  181. }
  182. /**
  183. * ???????? ???????.
  184. *
  185. * ?????? ?????? ?????? ????, ?.?. ?? ???????? ?? ???????, ???
  186. * ????????? ???????????????? ???????.
  187. */
  188. public function testNodeCheckPermissions()
  189. {
  190. $ctx = $this->ctx;
  191. $t = new Molinos_Database_Transaction();
  192. $node = Molinos_Core_API::getInstance('node')->create(array(
  193. 'class' => 'dummy',
  194. ));
  195. $this->assertTrue($node->checkPermission(Molinos_Auth_ACL::CREATE));
  196. $this->assertTrue($node->checkPermission(Molinos_Auth_ACL::DELETE));
  197. }
  198. /**
  199. * ???????? ???????? ?????.
  200. *
  201. * ??????????? ??????????? action ? ??????? ??????? ?????????.
  202. */
  203. public function testNodeFormGet()
  204. {
  205. $ctx = $this->ctx;
  206. $t = new Molinos_Database_Transaction();
  207. $node = Molinos_Core_API::getInstance('node')->create(array(
  208. 'class' => 'dummy',
  209. ));
  210. $form = $node->getForm();
  211. $this->assertTrue($form instanceof Molinos_Form_Form);
  212. $this->assertEquals('api/node/create', $form->action);
  213. }
  214. public function testNodeGetFormAction()
  215. {
  216. $ctx = $this->ctx;
  217. $t = new Molinos_Database_Transaction();
  218. $node = Molinos_Core_API::getInstance('node')->create(array(
  219. 'class' => 'dummy',
  220. ));
  221. $this->assertEquals('api/node/create', $node->getFormAction());
  222. }
  223. public function testNodeFormProcess()
  224. {
  225. $ctx = $this->ctx;
  226. $t = new Molinos_Database_Transaction();
  227. $node = Molinos_Core_API::getInstance('node')->create(array(
  228. 'class' => 'dummy',
  229. ));
  230. $node->formProcess(array(
  231. 'foo' => 'bar',
  232. ));
  233. // ?????? ?? ?????? ????, ?.?. ??? ??????????????? ???? dummy ???? foo ?? ???????.
  234. $this->assertFalse(isset($node->foo));
  235. }
  236. /**
  237. * ???????? ?????? ??????????????? ??????.
  238. * @expectedException RuntimeException
  239. */
  240. public function testNodeBadMethodCall()
  241. {
  242. $ctx = $this->ctx;
  243. $t = new Molinos_Database_Transaction();
  244. $node = Molinos_Core_API::getInstance('node')->create(array(
  245. 'class' => 'dummy',
  246. ));
  247. $node->aMethodThatDoesNotExist();
  248. }
  249. /**
  250. * ???????? ????????? ?????.
  251. */
  252. public function testNodeGetSchema()
  253. {
  254. $ctx = $this->ctx;
  255. $t = new Molinos_Database_Transaction();
  256. $node = Molinos_Core_API::getInstance('node')->create(array(
  257. 'class' => 'dummy',
  258. ));
  259. $schema = $node->getFormFields();
  260. $this->assertTrue(is_array($schema), "Node::getFormFields() must have returned an array.");
  261. }
  262. public function testNodeGetActionLinks()
  263. {
  264. $ctx = $this->ctx;
  265. $t = new Molinos_Database_Transaction();
  266. $links = Molinos_Core_API::getInstance('node')->create(array(
  267. 'class' => 'dummy',
  268. ))->getActionLinks();
  269. $this->assertTrue(is_array($links));
  270. foreach (array('edit', 'delete', 'publish') as $action)
  271. $this->assertTrue(array_key_exists($action, $links));
  272. }
  273. public function testNodeGetActionLinksXML()
  274. {
  275. $ctx = $this->ctx;
  276. $t = new Molinos_Database_Transaction();
  277. $xml = Molinos_Core_API::getInstance('node')->create(array(
  278. 'class' => 'dummy',
  279. ))->getActionLinksXML();
  280. $this->assertTrue(0 === strpos($xml, '<links>'));
  281. }
  282. public function testNodeGetSortedLis()
  283. {
  284. $ctx = $this->ctx;
  285. $nodeapi = Molinos_Core_API::getInstance('node');
  286. $t = new Molinos_Database_Transaction();
  287. $class = $this->getNewType();
  288. $nodeapi->create(array('class' => $class, 'name' => 'z'))->save();
  289. $nodeapi->create(array('class' => $class, 'name' => 'y'))->save();
  290. $nodeapi->create(array('class' => $class, 'name' => 'x'))->save();
  291. $list = $nodeapi->getSortedList($class);
  292. $this->assertTrue(is_array($list));
  293. $this->assertFalse(empty($list));
  294. $list = $nodeapi->getSortedList('dummyx');
  295. $this->assertTrue(is_array($list));
  296. $this->assertTrue(empty($list));
  297. }
  298. public function testNodeGetName()
  299. {
  300. $nodeapi = Molinos_Core_API::getInstance('node');
  301. $node = $nodeapi->create(array(
  302. 'class' => 'dummy',
  303. ));
  304. $this->assertTrue(null === $node->getName());
  305. $node = $nodeapi->create(array(
  306. 'class' => 'dummy',
  307. 'name' => 'xyz',
  308. ));
  309. $this->assertTrue('xyz' === $node->name);
  310. }
  311. public function testNodeGetEnabledSections()
  312. {
  313. $t = new Molinos_Database_Transaction();
  314. $nodeapi = Molinos_Core_API::getInstance('node');
  315. $node = $nodeapi->create(array(
  316. 'class' => 'dummy',
  317. ));
  318. $list = $node->getEnabledSections();
  319. $this->assertTrue(is_array($list));
  320. $this->assertTrue(empty($list));
  321. $node = $nodeapi->create(array(
  322. 'class' => 'article',
  323. ));
  324. $list = $node->getEnabledSections();
  325. $this->assertTrue(is_array($list));
  326. $this->assertTrue(empty($list));
  327. }
  328. /**
  329. * @expectedException RuntimeException
  330. */
  331. public function testNodeGetImage()
  332. {
  333. $node = Molinos_Core_API::getInstance('node')->create(array(
  334. 'class' => 'dummy',
  335. ));
  336. $node->getImage();
  337. }
  338. /**
  339. * ???????? ?????????????? ?????????? ?????? ?????. ???????????? ?????
  340. * ?? ????????? ???? ?? ??????, ??? ?????????? ????????????? ?????????????.
  341. * ? ????????? ?????? ???????? ???????? ? ????????? ??? ?? ????.
  342. */
  343. public function testNodeNoRequiredDefaultFields()
  344. {
  345. $node = Molinos_Core_API::getInstance('node')->create(array(
  346. 'class' => 'dummy',
  347. ));
  348. foreach ($node->getFormFields() as $k => $v)
  349. $this->assertTrue(empty($v['required']), "Must have no mandatory fields in the default schema, but {$k} is.");
  350. }
  351. /**
  352. * ???????? ?????? ?? ?????? ??????? ??? ? ????????? ???????????.
  353. */
  354. public function testNodeListURI()
  355. {
  356. $t = new Molinos_Database_Transaction();
  357. $nodeapi = Molinos_Core_API::getInstance('node');
  358. $type = $nodeapi->create(array(
  359. 'class' => 'type',
  360. 'name' => 'testlisturi',
  361. 'deleted' => 0,
  362. 'isdictionary' => 0,
  363. ))->save();
  364. $node = $nodeapi->create(array(
  365. 'class' => 'testlisturi',
  366. ));
  367. $this->assertEquals('admin/content/list/testlisturi', $node->getListURL());
  368. $type->isdictionary = true;
  369. $type->save();
  370. $this->assertEquals('admin/content/dict/testlisturi', $node->getListURL());
  371. }
  372. /**
  373. * ???????? ?????? getParentIds().
  374. *
  375. * ??????? ????? ???????? ??????? ??????????? ????, ????? ??????? ????????
  376. * ??????? (?? ????????) ? ????????? ???????????? ??????? ????????. ??????
  377. * ???? ?????? ? ????? ????????????? ???????. ????? ???????? ???????
  378. * ??????????? ? ???????? ???????????.
  379. *
  380. * @url http://code.google.com/p/molinos-cms/issues/detail?id=1895#c7
  381. */
  382. public function testNodeGetParentIds()
  383. {
  384. $t = new Molinos_Database_Transaction();
  385. $api = Molinos_Core_API::getInstance('node');
  386. $type = $this->getNewType();
  387. $root = $api->create(array(
  388. 'class' => $type,
  389. ))->save();
  390. $child1 = $api->create(array(
  391. 'class' => $type,
  392. 'parent_id' => $root->id,
  393. ));
  394. $children1 = $child1->getParentIds();
  395. $child1->save();
  396. $child2 = $api->create(array(
  397. 'class' => $type,
  398. 'parent_id' => $root->id,
  399. ));
  400. $children2 = $child2->getParentIds();
  401. $child2->save();
  402. $wanted = array($root->id);
  403. $this->assertEquals($wanted, $children1);
  404. $this->assertEquals($wanted, $children2);
  405. }
  406. /**
  407. * ???????? ????????? ?????? ?????????????? ? api/url/modify.
  408. *
  409. * ?????????? ??? ?????-???? ??????????.
  410. *
  411. * @expectedException Molinos_HTTP_Exceptions_BadRequest
  412. */
  413. public function testURLModifierNoArguments()
  414. {
  415. $this->do_get('api/url/modify');
  416. }
  417. public function testURLModifierChangePath()
  418. {
  419. $res = $this->do_get('api/url/modify', array(
  420. 'from' => '/submit/file/ftp?destination=%2Fnode%2F6637%2Fpreview&sendto=6637',
  421. 'path' => 'submit/file/local',
  422. ));
  423. $this->assertEquals('/submit/file/local?destination=%2Fnode%2F6637%2Fpreview&sendto=6637', $res->getLocation($this->ctx));
  424. }
  425. public function testTempFolderCleanUp()
  426. {
  427. $dirName = Molinos_Core_Utils::tmpdir();
  428. $now = time();
  429. touch($fn1 = $dirName . DIRECTORY_SEPARATOR . 'testfile1', $now);
  430. touch($fn2 = $dirName . DIRECTORY_SEPARATOR . 'testfile2', $now - 2592000);
  431. $this->assertEquals($now, filemtime($fn1));
  432. $this->assertEquals($now - 2592000, filemtime($fn2));
  433. $this->do_get('cron/temp-files-cleaner');
  434. $this->assertTrue(file_exists($fn1));
  435. $this->assertFalse(file_exists($fn2));
  436. unlink($fn1);
  437. }
  438. }