RssHelperTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. <?php
  2. /**
  3. * RssHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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->assertEqual($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. }
  417. /**
  418. * test item() with cdata blocks.
  419. *
  420. * @return void
  421. */
  422. public function testItemCdata() {
  423. $item = array(
  424. 'title' => array(
  425. 'value' => 'My Title & more',
  426. 'cdata' => true,
  427. 'convertEntities' => false,
  428. )
  429. );
  430. $result = $this->Rss->item(null, $item);
  431. $expected = array(
  432. '<item',
  433. '<title',
  434. '<![CDATA[My Title & more]]',
  435. '/title',
  436. '/item'
  437. );
  438. $this->assertTags($result, $expected);
  439. $item = array(
  440. 'category' => array(
  441. 'value' => 'CakePHP',
  442. 'cdata' => true,
  443. 'domain' => 'http://www.cakephp.org',
  444. )
  445. );
  446. $result = $this->Rss->item(null, $item);
  447. $expected = array(
  448. '<item',
  449. 'category' => array('domain' => 'http://www.cakephp.org'),
  450. '<![CDATA[CakePHP]]',
  451. '/category',
  452. '/item'
  453. );
  454. $this->assertTags($result, $expected);
  455. $item = array(
  456. 'category' => array(
  457. array(
  458. 'value' => 'CakePHP',
  459. 'cdata' => true,
  460. 'domain' => 'http://www.cakephp.org'
  461. ),
  462. array(
  463. 'value' => 'Bakery',
  464. 'cdata' => true
  465. )
  466. )
  467. );
  468. $result = $this->Rss->item(null, $item);
  469. $expected = array(
  470. '<item',
  471. 'category' => array('domain' => 'http://www.cakephp.org'),
  472. '<![CDATA[CakePHP]]',
  473. '/category',
  474. '<category',
  475. '<![CDATA[Bakery]]',
  476. '/category',
  477. '/item'
  478. );
  479. $this->assertTags($result, $expected);
  480. $item = array(
  481. 'title' => array(
  482. 'value' => 'My Title',
  483. 'cdata' => true,
  484. ),
  485. 'link' => 'http://www.example.com/1',
  486. 'description' => array(
  487. 'value' => 'descriptive words',
  488. 'cdata' => true,
  489. ),
  490. 'enclosure' => array(
  491. 'url' => '/test.flv'
  492. ),
  493. 'pubDate' => '2008-05-31 12:00:00',
  494. 'guid' => 'http://www.example.com/1',
  495. 'category' => array(
  496. array(
  497. 'value' => 'CakePHP',
  498. 'cdata' => true,
  499. 'domain' => 'http://www.cakephp.org'
  500. ),
  501. array(
  502. 'value' => 'Bakery',
  503. 'cdata' => true
  504. )
  505. )
  506. );
  507. $result = $this->Rss->item(null, $item);
  508. $expected = array(
  509. '<item',
  510. '<title',
  511. '<![CDATA[My Title]]',
  512. '/title',
  513. '<link',
  514. 'http://www.example.com/1',
  515. '/link',
  516. '<description',
  517. '<![CDATA[descriptive words]]',
  518. '/description',
  519. 'enclosure' => array('url' => $this->Rss->url('/test.flv', true)),
  520. '<pubDate',
  521. date('r', strtotime('2008-05-31 12:00:00')),
  522. '/pubDate',
  523. '<guid',
  524. 'http://www.example.com/1',
  525. '/guid',
  526. 'category' => array('domain' => 'http://www.cakephp.org'),
  527. '<![CDATA[CakePHP]]',
  528. '/category',
  529. '<category',
  530. '<![CDATA[Bakery]]',
  531. '/category',
  532. '/item'
  533. );
  534. $this->assertTags($result, $expected);
  535. }
  536. /**
  537. * test item() with enclosure data.
  538. *
  539. * @return void
  540. */
  541. public function testItemEnclosureLength() {
  542. $tmpFile = $this->_getWwwTmpFile();
  543. if (file_exists($tmpFile)) {
  544. unlink($tmpFile);
  545. }
  546. $File = new File($tmpFile, true, '0777');
  547. $this->assertTrue($File->write('123'), 'Could not write to ' . $tmpFile);
  548. clearstatcache(true, $tmpFile);
  549. $item = array(
  550. 'title' => array(
  551. 'value' => 'My Title',
  552. 'cdata' => true,
  553. ),
  554. 'link' => 'http://www.example.com/1',
  555. 'description' => array(
  556. 'value' => 'descriptive words',
  557. 'cdata' => true,
  558. ),
  559. 'enclosure' => array(
  560. 'url' => '/tests/cakephp.file.test.tmp'
  561. ),
  562. 'pubDate' => '2008-05-31 12:00:00',
  563. 'guid' => 'http://www.example.com/1',
  564. 'category' => array(
  565. array(
  566. 'value' => 'CakePHP',
  567. 'cdata' => true,
  568. 'domain' => 'http://www.cakephp.org'
  569. ),
  570. array(
  571. 'value' => 'Bakery',
  572. 'cdata' => true
  573. )
  574. )
  575. );
  576. $result = $this->Rss->item(null, $item);
  577. $expected = array(
  578. '<item',
  579. '<title',
  580. '<![CDATA[My Title]]',
  581. '/title',
  582. '<link',
  583. 'http://www.example.com/1',
  584. '/link',
  585. '<description',
  586. '<![CDATA[descriptive words]]',
  587. '/description',
  588. 'enclosure' => array(
  589. 'url' => $this->Rss->url('/tests/cakephp.file.test.tmp', true),
  590. 'length' => filesize($tmpFile),
  591. 'type' => 'text/plain'
  592. ),
  593. '<pubDate',
  594. date('r', strtotime('2008-05-31 12:00:00')),
  595. '/pubDate',
  596. '<guid',
  597. 'http://www.example.com/1',
  598. '/guid',
  599. 'category' => array('domain' => 'http://www.cakephp.org'),
  600. '<![CDATA[CakePHP]]',
  601. '/category',
  602. '<category',
  603. '<![CDATA[Bakery]]',
  604. '/category',
  605. '/item'
  606. );
  607. $this->assertTags($result, $expected);
  608. unlink($tmpFile);
  609. }
  610. /**
  611. * testTime method
  612. *
  613. * @return void
  614. */
  615. public function testTime() {
  616. }
  617. /**
  618. * testElementAttrNotInParent method
  619. *
  620. * @return void
  621. */
  622. public function testElementAttrNotInParent() {
  623. $attributes = array(
  624. 'title' => 'Some Title',
  625. 'link' => 'http://link.com',
  626. 'description' => 'description'
  627. );
  628. $elements = array('enclosure' => array('url' => 'http://test.com'));
  629. $result = $this->Rss->item($attributes, $elements);
  630. $expected = array(
  631. 'item' => array(
  632. 'title' => 'Some Title',
  633. 'link' => 'http://link.com',
  634. 'description' => 'description'
  635. ),
  636. 'enclosure' => array(
  637. 'url' => 'http://test.com'
  638. ),
  639. '/item'
  640. );
  641. $this->assertTags($result, $expected);
  642. }
  643. /**
  644. * getWwwTmpFile method
  645. *
  646. * @param bool $paintSkip
  647. * @return void
  648. */
  649. function _getWwwTmpFile() {
  650. $path = WWW_ROOT . 'tests' . DS;
  651. $tmpFile = $path. 'cakephp.file.test.tmp';
  652. if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
  653. return $tmpFile;
  654. };
  655. $message = __d('cake_dev', '%s is not writeable', $path );
  656. $this->markTestSkipped($message);
  657. return false;
  658. }
  659. }