PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/View/Helper/RssHelperTest.php

https://bitbucket.org/praveen_excell/opshop
PHP | 778 lines | 667 code | 39 blank | 72 comment | 7 complexity | ccffdcc114c20ef85e927f174d6f8658 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * RssHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('View', 'View');
  20. App::uses('RssHelper', 'View/Helper');
  21. App::uses('TimeHelper', 'View/Helper');
  22. App::uses('File', 'Utility');
  23. /**
  24. * RssHelperTest class
  25. *
  26. * @package Cake.Test.Case.View.Helper
  27. */
  28. class RssHelperTest extends CakeTestCase {
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. $controller = null;
  37. $this->View = new View($controller);
  38. $this->Rss = new RssHelper($this->View);
  39. }
  40. /**
  41. * tearDown method
  42. *
  43. * @return void
  44. */
  45. public function tearDown() {
  46. parent::tearDown();
  47. unset($this->Rss);
  48. }
  49. /**
  50. * testDocument method
  51. *
  52. * @return void
  53. */
  54. public function testDocument() {
  55. $result = $this->Rss->document();
  56. $expected = array(
  57. 'rss' => array(
  58. 'version' => '2.0'
  59. )
  60. );
  61. $this->assertTags($result, $expected);
  62. $result = $this->Rss->document(null, 'content');
  63. $expected = array(
  64. 'rss' => array(
  65. 'version' => '2.0'
  66. ),
  67. 'content'
  68. );
  69. $this->assertTags($result, $expected);
  70. $result = $this->Rss->document(array('contrived' => 'parameter'), 'content');
  71. $expected = array(
  72. 'rss' => array(
  73. 'contrived' => 'parameter',
  74. 'version' => '2.0'
  75. ),
  76. 'content'
  77. );
  78. $this->assertTags($result, $expected);
  79. }
  80. /**
  81. * testChannel method
  82. *
  83. * @return void
  84. */
  85. public function testChannel() {
  86. $attrib = array('a' => '1', 'b' => '2');
  87. $elements = array('title' => 'title');
  88. $content = 'content';
  89. $result = $this->Rss->channel($attrib, $elements, $content);
  90. $expected = array(
  91. 'channel' => array(
  92. 'a' => '1',
  93. 'b' => '2'
  94. ),
  95. '<title',
  96. 'title',
  97. '/title',
  98. '<link',
  99. $this->Rss->url('/', true),
  100. '/link',
  101. '<description',
  102. 'content',
  103. '/channel'
  104. );
  105. $this->assertTags($result, $expected);
  106. $this->View->pageTitle = 'title';
  107. $attrib = array('a' => '1', 'b' => '2');
  108. $elements = array();
  109. $content = 'content';
  110. $result = $this->Rss->channel($attrib, $elements, $content);
  111. $expected = array(
  112. 'channel' => array(
  113. 'a' => '1',
  114. 'b' => '2'
  115. ),
  116. '<title',
  117. 'title',
  118. '/title',
  119. '<link',
  120. $this->Rss->url('/', true),
  121. '/link',
  122. '<description',
  123. 'content',
  124. '/channel'
  125. );
  126. $this->assertTags($result, $expected);
  127. }
  128. /**
  129. * test correct creation of channel sub elements.
  130. *
  131. * @return void
  132. */
  133. public function testChannelElements() {
  134. $attrib = array();
  135. $elements = array(
  136. 'title' => 'Title of RSS Feed',
  137. 'link' => 'http://example.com',
  138. 'description' => 'Description of RSS Feed',
  139. 'image' => array(
  140. 'title' => 'Title of image',
  141. 'url' => 'http://example.com/example.png',
  142. 'link' => 'http://example.com'
  143. ),
  144. 'cloud' => array(
  145. 'domain' => "rpc.sys.com",
  146. 'port' => "80",
  147. 'path' => "/RPC2",
  148. 'registerProcedure' => "myCloud.rssPleaseNotify",
  149. 'protocol' => "xml-rpc"
  150. )
  151. );
  152. $content = 'content-here';
  153. $result = $this->Rss->channel($attrib, $elements, $content);
  154. $expected = array(
  155. '<channel',
  156. '<title', 'Title of RSS Feed', '/title',
  157. '<link', 'http://example.com', '/link',
  158. '<description', 'Description of RSS Feed', '/description',
  159. '<image',
  160. '<title', 'Title of image', '/title',
  161. '<url', 'http://example.com/example.png', '/url',
  162. '<link', 'http://example.com', '/link',
  163. '/image',
  164. 'cloud' => array(
  165. 'domain' => "rpc.sys.com",
  166. 'port' => "80",
  167. 'path' => "/RPC2",
  168. 'registerProcedure' => "myCloud.rssPleaseNotify",
  169. 'protocol' => "xml-rpc"
  170. ),
  171. 'content-here',
  172. '/channel',
  173. );
  174. $this->assertTags($result, $expected);
  175. }
  176. public function testChannelElementAttributes() {
  177. $attrib = array();
  178. $elements = array(
  179. 'title' => 'Title of RSS Feed',
  180. 'link' => 'http://example.com',
  181. 'description' => 'Description of RSS Feed',
  182. 'image' => array(
  183. 'title' => 'Title of image',
  184. 'url' => 'http://example.com/example.png',
  185. 'link' => 'http://example.com'
  186. ),
  187. 'atom:link' => array(
  188. 'attrib' => array(
  189. 'href' => 'http://www.example.com/rss.xml',
  190. 'rel' => 'self',
  191. 'type' => 'application/rss+xml')
  192. )
  193. );
  194. $content = 'content-here';
  195. $result = $this->Rss->channel($attrib, $elements, $content);
  196. $expected = array(
  197. '<channel',
  198. '<title', 'Title of RSS Feed', '/title',
  199. '<link', 'http://example.com', '/link',
  200. '<description', 'Description of RSS Feed', '/description',
  201. '<image',
  202. '<title', 'Title of image', '/title',
  203. '<url', 'http://example.com/example.png', '/url',
  204. '<link', 'http://example.com', '/link',
  205. '/image',
  206. 'atom:link' => array(
  207. 'xmlns:atom' => 'http://www.w3.org/2005/Atom',
  208. 'href' => "http://www.example.com/rss.xml",
  209. 'rel' => "self",
  210. 'type' => "application/rss+xml"
  211. ),
  212. 'content-here',
  213. '/channel',
  214. );
  215. $this->assertTags($result, $expected);
  216. }
  217. /**
  218. * testItems method
  219. *
  220. * @return void
  221. */
  222. public function testItems() {
  223. $items = array(
  224. array('title' => 'title1', 'guid' => 'http://www.example.com/guid1', 'link' => 'http://www.example.com/link1', 'description' => 'description1'),
  225. array('title' => 'title2', 'guid' => 'http://www.example.com/guid2', 'link' => 'http://www.example.com/link2', 'description' => 'description2'),
  226. array('title' => 'title3', 'guid' => 'http://www.example.com/guid3', 'link' => 'http://www.example.com/link3', 'description' => 'description3')
  227. );
  228. $result = $this->Rss->items($items);
  229. $expected = array(
  230. '<item',
  231. '<title', 'title1', '/title',
  232. '<guid', 'http://www.example.com/guid1', '/guid',
  233. '<link', 'http://www.example.com/link1', '/link',
  234. '<description', 'description1', '/description',
  235. '/item',
  236. '<item',
  237. '<title', 'title2', '/title',
  238. '<guid', 'http://www.example.com/guid2', '/guid',
  239. '<link', 'http://www.example.com/link2', '/link',
  240. '<description', 'description2', '/description',
  241. '/item',
  242. '<item',
  243. '<title', 'title3', '/title',
  244. '<guid', 'http://www.example.com/guid3', '/guid',
  245. '<link', 'http://www.example.com/link3', '/link',
  246. '<description', 'description3', '/description',
  247. '/item'
  248. );
  249. $this->assertTags($result, $expected);
  250. $items = array(
  251. array('title' => 'title1', 'guid' => 'http://www.example.com/guid1', 'link' => 'http://www.example.com/link1', 'description' => 'description1'),
  252. array('title' => 'title2', 'guid' => 'http://www.example.com/guid2', 'link' => 'http://www.example.com/link2', 'description' => 'description2'),
  253. array('title' => 'title3', 'guid' => 'http://www.example.com/guid3', 'link' => 'http://www.example.com/link3', 'description' => 'description3')
  254. );
  255. $result = $this->Rss->items($items, create_function('$v', '$v[\'title\'] = $v[\'title\'] . \'-transformed\'; return $v;'));
  256. $expected = array(
  257. '<item',
  258. '<title', 'title1-transformed', '/title',
  259. '<guid', 'http://www.example.com/guid1', '/guid',
  260. '<link', 'http://www.example.com/link1', '/link',
  261. '<description', 'description1', '/description',
  262. '/item',
  263. '<item',
  264. '<title', 'title2-transformed', '/title',
  265. '<guid', 'http://www.example.com/guid2', '/guid',
  266. '<link', 'http://www.example.com/link2', '/link',
  267. '<description', 'description2', '/description',
  268. '/item',
  269. '<item',
  270. '<title', 'title3-transformed', '/title',
  271. '<guid', 'http://www.example.com/guid3', '/guid',
  272. '<link', 'http://www.example.com/link3', '/link',
  273. '<description', 'description3', '/description',
  274. '/item'
  275. );
  276. $this->assertTags($result, $expected);
  277. $result = $this->Rss->items(array());
  278. $expected = '';
  279. $this->assertEquals($expected, $result);
  280. }
  281. /**
  282. * testItem method
  283. *
  284. * @return void
  285. */
  286. public function testItem() {
  287. $item = array(
  288. 'title' => 'My title',
  289. 'description' => 'My description',
  290. 'link' => 'http://www.google.com/'
  291. );
  292. $result = $this->Rss->item(null, $item);
  293. $expected = array(
  294. '<item',
  295. '<title',
  296. 'My title',
  297. '/title',
  298. '<description',
  299. 'My description',
  300. '/description',
  301. '<link',
  302. 'http://www.google.com/',
  303. '/link',
  304. '<guid',
  305. 'http://www.google.com/',
  306. '/guid',
  307. '/item'
  308. );
  309. $this->assertTags($result, $expected);
  310. $item = array(
  311. 'title' => 'My Title',
  312. 'link' => 'http://www.example.com/1',
  313. 'description' => 'descriptive words',
  314. 'pubDate' => '2008-05-31 12:00:00',
  315. 'source' => array('http://www.google.com/', 'Google'),
  316. 'guid' => 'http://www.example.com/1'
  317. );
  318. $result = $this->Rss->item(null, $item);
  319. $expected = array(
  320. '<item',
  321. '<title',
  322. 'My Title',
  323. '/title',
  324. '<link',
  325. 'http://www.example.com/1',
  326. '/link',
  327. '<description',
  328. 'descriptive words',
  329. '/description',
  330. '<pubDate',
  331. date('r', strtotime('2008-05-31 12:00:00')),
  332. '/pubDate',
  333. 'source' => array('url' => 'http://www.google.com/'),
  334. 'Google',
  335. '/source',
  336. '<guid',
  337. 'http://www.example.com/1',
  338. '/guid',
  339. '/item'
  340. );
  341. $this->assertTags($result, $expected);
  342. $item = array(
  343. 'title' => 'My Title & more'
  344. );
  345. $result = $this->Rss->item(null, $item);
  346. $expected = array(
  347. '<item',
  348. '<title', 'My Title &amp; more', '/title',
  349. '/item'
  350. );
  351. $this->assertTags($result, $expected);
  352. $item = array(
  353. 'title' => 'Foo bar',
  354. 'link' => array(
  355. 'url' => 'http://example.com/foo?a=1&b=2',
  356. 'convertEntities' => false
  357. ),
  358. 'description' => array(
  359. 'value' => 'descriptive words',
  360. 'cdata' => true,
  361. ),
  362. 'pubDate' => '2008-05-31 12:00:00',
  363. 'source' => 'http://www.google.com/'
  364. );
  365. $result = $this->Rss->item(null, $item);
  366. $expected = array(
  367. '<item',
  368. '<title',
  369. 'Foo bar',
  370. '/title',
  371. '<link',
  372. 'http://example.com/foo?a=1&amp;b=2',
  373. '/link',
  374. '<description',
  375. '<![CDATA[descriptive words]]',
  376. '/description',
  377. '<pubDate',
  378. date('r', strtotime('2008-05-31 12:00:00')),
  379. '/pubDate',
  380. '<source',
  381. 'http://www.google.com/',
  382. '/source',
  383. '<guid',
  384. 'http://example.com/foo?a=1&amp;b=2',
  385. '/guid',
  386. '/item'
  387. );
  388. $this->assertTags($result, $expected);
  389. $item = array(
  390. 'title' => 'My title',
  391. 'description' => 'My description',
  392. 'link' => 'http://www.google.com/',
  393. 'source' => array('url' => 'http://www.example.com/', 'title' => 'Example website')
  394. );
  395. $result = $this->Rss->item(null, $item);
  396. $expected = array(
  397. '<item',
  398. '<title',
  399. 'My title',
  400. '/title',
  401. '<description',
  402. 'My description',
  403. '/description',
  404. '<link',
  405. 'http://www.google.com/',
  406. '/link',
  407. 'source' => array('url' => 'http://www.example.com/'),
  408. 'Example website',
  409. '/source',
  410. '<guid',
  411. 'http://www.google.com/',
  412. '/guid',
  413. '/item'
  414. );
  415. $this->assertTags($result, $expected);
  416. $item = array(
  417. 'title' => 'My title',
  418. 'description' => 'My description',
  419. 'link' => 'http://www.google.com/',
  420. 'category' => array('Category One', 'Category Two')
  421. );
  422. $result = $this->Rss->item(null, $item);
  423. $expected = array(
  424. '<item',
  425. '<title',
  426. 'My title',
  427. '/title',
  428. '<description',
  429. 'My description',
  430. '/description',
  431. '<link',
  432. 'http://www.google.com/',
  433. '/link',
  434. '<category',
  435. 'Category One',
  436. '/category',
  437. '<category',
  438. 'Category Two',
  439. '/category',
  440. '<guid',
  441. 'http://www.google.com/',
  442. '/guid',
  443. '/item'
  444. );
  445. $this->assertTags($result, $expected);
  446. }
  447. /**
  448. * test item() with cdata blocks.
  449. *
  450. * @return void
  451. */
  452. public function testItemCdata() {
  453. $item = array(
  454. 'title' => array(
  455. 'value' => 'My Title & more',
  456. 'cdata' => true,
  457. 'convertEntities' => false,
  458. )
  459. );
  460. $result = $this->Rss->item(null, $item);
  461. $expected = array(
  462. '<item',
  463. '<title',
  464. '<![CDATA[My Title & more]]',
  465. '/title',
  466. '/item'
  467. );
  468. $this->assertTags($result, $expected);
  469. $item = array(
  470. 'category' => array(
  471. 'value' => 'CakePHP',
  472. 'cdata' => true,
  473. 'domain' => 'http://www.cakephp.org',
  474. )
  475. );
  476. $result = $this->Rss->item(null, $item);
  477. $expected = array(
  478. '<item',
  479. 'category' => array('domain' => 'http://www.cakephp.org'),
  480. '<![CDATA[CakePHP]]',
  481. '/category',
  482. '/item'
  483. );
  484. $this->assertTags($result, $expected);
  485. $item = array(
  486. 'category' => array(
  487. array(
  488. 'value' => 'CakePHP',
  489. 'cdata' => true,
  490. 'domain' => 'http://www.cakephp.org'
  491. ),
  492. array(
  493. 'value' => 'Bakery',
  494. 'cdata' => true
  495. )
  496. )
  497. );
  498. $result = $this->Rss->item(null, $item);
  499. $expected = array(
  500. '<item',
  501. 'category' => array('domain' => 'http://www.cakephp.org'),
  502. '<![CDATA[CakePHP]]',
  503. '/category',
  504. '<category',
  505. '<![CDATA[Bakery]]',
  506. '/category',
  507. '/item'
  508. );
  509. $this->assertTags($result, $expected);
  510. $item = array(
  511. 'title' => array(
  512. 'value' => 'My Title',
  513. 'cdata' => true,
  514. ),
  515. 'link' => 'http://www.example.com/1',
  516. 'description' => array(
  517. 'value' => 'descriptive words',
  518. 'cdata' => true,
  519. ),
  520. 'enclosure' => array(
  521. 'url' => '/test.flv'
  522. ),
  523. 'pubDate' => '2008-05-31 12:00:00',
  524. 'guid' => 'http://www.example.com/1',
  525. 'category' => array(
  526. array(
  527. 'value' => 'CakePHP',
  528. 'cdata' => true,
  529. 'domain' => 'http://www.cakephp.org'
  530. ),
  531. array(
  532. 'value' => 'Bakery',
  533. 'cdata' => true
  534. )
  535. )
  536. );
  537. $result = $this->Rss->item(null, $item);
  538. $expected = array(
  539. '<item',
  540. '<title',
  541. '<![CDATA[My Title]]',
  542. '/title',
  543. '<link',
  544. 'http://www.example.com/1',
  545. '/link',
  546. '<description',
  547. '<![CDATA[descriptive words]]',
  548. '/description',
  549. 'enclosure' => array('url' => $this->Rss->url('/test.flv', true)),
  550. '<pubDate',
  551. date('r', strtotime('2008-05-31 12:00:00')),
  552. '/pubDate',
  553. '<guid',
  554. 'http://www.example.com/1',
  555. '/guid',
  556. 'category' => array('domain' => 'http://www.cakephp.org'),
  557. '<![CDATA[CakePHP]]',
  558. '/category',
  559. '<category',
  560. '<![CDATA[Bakery]]',
  561. '/category',
  562. '/item'
  563. );
  564. $this->assertTags($result, $expected);
  565. }
  566. /**
  567. * test item() with enclosure data.
  568. *
  569. * @return void
  570. */
  571. public function testItemEnclosureLength() {
  572. if (!is_writable(WWW_ROOT)) {
  573. $this->markTestSkipped(__d('cake_dev', 'Webroot is not writable.'));
  574. }
  575. $testExists = is_dir(WWW_ROOT . 'tests');
  576. $tmpFile = WWW_ROOT . 'tests' . DS . 'cakephp.file.test.tmp';
  577. $File = new File($tmpFile, true);
  578. $this->assertTrue($File->write('123'), 'Could not write to ' . $tmpFile);
  579. if (50300 <= PHP_VERSION_ID) {
  580. clearstatcache(true, $tmpFile);
  581. } else {
  582. clearstatcache();
  583. }
  584. $item = array(
  585. 'title' => array(
  586. 'value' => 'My Title',
  587. 'cdata' => true,
  588. ),
  589. 'link' => 'http://www.example.com/1',
  590. 'description' => array(
  591. 'value' => 'descriptive words',
  592. 'cdata' => true,
  593. ),
  594. 'enclosure' => array(
  595. 'url' => '/tests/cakephp.file.test.tmp'
  596. ),
  597. 'pubDate' => '2008-05-31 12:00:00',
  598. 'guid' => 'http://www.example.com/1',
  599. 'category' => array(
  600. array(
  601. 'value' => 'CakePHP',
  602. 'cdata' => true,
  603. 'domain' => 'http://www.cakephp.org'
  604. ),
  605. array(
  606. 'value' => 'Bakery',
  607. 'cdata' => true
  608. )
  609. )
  610. );
  611. $result = $this->Rss->item(null, $item);
  612. if (!function_exists('finfo_open') &&
  613. (function_exists('mime_content_type') && false === mime_content_type($tmpFile))
  614. ) {
  615. $type = false;
  616. } else {
  617. $type = 'text/plain';
  618. }
  619. $expected = array(
  620. '<item',
  621. '<title',
  622. '<![CDATA[My Title]]',
  623. '/title',
  624. '<link',
  625. 'http://www.example.com/1',
  626. '/link',
  627. '<description',
  628. '<![CDATA[descriptive words]]',
  629. '/description',
  630. 'enclosure' => array(
  631. 'url' => $this->Rss->url('/tests/cakephp.file.test.tmp', true),
  632. 'length' => filesize($tmpFile),
  633. 'type' => $type
  634. ),
  635. '<pubDate',
  636. date('r', strtotime('2008-05-31 12:00:00')),
  637. '/pubDate',
  638. '<guid',
  639. 'http://www.example.com/1',
  640. '/guid',
  641. 'category' => array('domain' => 'http://www.cakephp.org'),
  642. '<![CDATA[CakePHP]]',
  643. '/category',
  644. '<category',
  645. '<![CDATA[Bakery]]',
  646. '/category',
  647. '/item'
  648. );
  649. $this->assertTags($result, $expected);
  650. $File->delete();
  651. if (!$testExists) {
  652. $Folder = new Folder(WWW_ROOT . 'tests');
  653. $Folder->delete();
  654. }
  655. }
  656. /**
  657. * testElementAttrNotInParent method
  658. *
  659. * @return void
  660. */
  661. public function testElementAttrNotInParent() {
  662. $attributes = array(
  663. 'title' => 'Some Title',
  664. 'link' => 'http://link.com',
  665. 'description' => 'description'
  666. );
  667. $elements = array('enclosure' => array('url' => 'http://test.com'));
  668. $result = $this->Rss->item($attributes, $elements);
  669. $expected = array(
  670. 'item' => array(
  671. 'title' => 'Some Title',
  672. 'link' => 'http://link.com',
  673. 'description' => 'description'
  674. ),
  675. 'enclosure' => array(
  676. 'url' => 'http://test.com'
  677. ),
  678. '/item'
  679. );
  680. $this->assertTags($result, $expected);
  681. }
  682. public function testElementNamespaceWithoutPrefix() {
  683. $item = array(
  684. 'creator' => 'Alex',
  685. );
  686. $attributes = array(
  687. 'namespace' => 'http://link.com'
  688. );
  689. $result = $this->Rss->item($attributes, $item);
  690. $expected = array(
  691. 'item' => array(
  692. 'xmlns' => 'http://link.com'
  693. ),
  694. 'creator' => array(
  695. 'xmlns' => 'http://link.com'
  696. ),
  697. 'Alex',
  698. '/creator',
  699. '/item'
  700. );
  701. $this->assertTags($result, $expected, true);
  702. }
  703. public function testElementNamespaceWithPrefix() {
  704. $item = array(
  705. 'title' => 'Title',
  706. 'dc:creator' => 'Alex',
  707. 'xy:description' => 'descriptive words'
  708. );
  709. $attributes = array(
  710. 'namespace' => array(
  711. 'prefix' => 'dc',
  712. 'url' => 'http://link.com'
  713. )
  714. );
  715. $result = $this->Rss->item($attributes, $item);
  716. $expected = array(
  717. 'item' => array(
  718. 'xmlns:dc' => 'http://link.com'
  719. ),
  720. 'title' => array(
  721. 'xmlns:dc' => 'http://link.com'
  722. ),
  723. 'Title',
  724. '/title',
  725. 'dc:creator' => array(
  726. 'xmlns:dc' => 'http://link.com'
  727. ),
  728. 'Alex',
  729. '/dc:creator',
  730. 'description' => array(
  731. 'xmlns:dc' => 'http://link.com'
  732. ),
  733. 'descriptive words',
  734. '/description',
  735. '/item'
  736. );
  737. $this->assertTags($result, $expected, true);
  738. }
  739. }