PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/models/FeedsTest.php

https://bitbucket.org/skuda/rsslounge
PHP | 366 lines | 228 code | 71 blank | 67 comment | 0 complexity | 5e12ea86a0560ce668fab5776959afa2 MD5 | raw file
  1. <?PHP
  2. /**
  3. * UnitTest for the feed model class
  4. *
  5. * @package tests_models
  6. * @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
  7. * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
  8. */
  9. class FeedsTest extends PHPUnit_Framework_TestCase {
  10. /**
  11. * @var application_models_feeds
  12. */
  13. protected $model;
  14. /**
  15. * @var Zend_Db
  16. */
  17. protected $db;
  18. /**
  19. * prepare tests
  20. */
  21. public function setUp() {
  22. $this->model = new application_models_feeds();
  23. $this->db = $db = Zend_Registry::get('bootstrap')->getPluginResource('db')->getDbAdapter();
  24. // insert test data
  25. $this->db->exec("INSERT INTO " . Zend_Registry::get('config')->resources->db->prefix . 'categories' . " (`id`,`name`,`position`)
  26. VALUES
  27. (1 , 'category 1', '0'),
  28. (2 , 'category 2', '1'),
  29. (3 , 'category 3', '2');
  30. INSERT INTO " . Zend_Registry::get('config')->resources->db->prefix . 'feeds' . "
  31. (`id`, `source`, `url`, `category`, `priority`, `favicon`, `filter`, `name`, `position`, `icon`, `multimedia`, `dirtyicon`, `htmlurl`, `lastrefresh`, `error`)
  32. VALUES
  33. (1, 'plugins_rss_feed', 'http://www.n-tv.de/wirtschaft/rss', '1', '1', 'http://rsslounge.aditu.de/favicon.ico', NULL, 'n-tv', '0', '', '0', '1', '', NULL, '0'),
  34. (2, 'plugins_rss_feed', 'http://newsfeed.zeit.de/', '1', '2', '', NULL, 'Zeit Online', '2', '', '1', '1', 'http://blog.aditu.de/', NULL, '0'),
  35. (3, 'plugins_rss_feed', 'http://bcaptured.tumblr.co', '1', '2', '', NULL, 'bCaptured Tumblr', '3', '4711.ico', '1', '1', '', NULL, '0');
  36. INSERT INTO " . Zend_Registry::get('config')->resources->db->prefix . 'items' . "
  37. (`id`, `title`, `content`, `feed`, `unread`, `starred`, `datetime`, `uid`, `link`)
  38. VALUES
  39. (NULL, 'testitem 1', 'testcontent blabla', '1', '1', '0', '2009-09-07 15:47:30', '4711', 'http://www.aditu.de');
  40. INSERT INTO " . Zend_Registry::get('config')->resources->db->prefix . 'messages' . "
  41. (`id`, `feed`, `datetime`, `message`)
  42. VALUES
  43. (NULL, '3', '2009-09-09 16:27:39', 'Fehler 1'),
  44. (NULL, '3', '2009-09-07 16:27:48', 'Bla Fehler');
  45. ");
  46. $handle = fopen (Zend_Registry::get('config')->favicons->path."4711.ico", "w");
  47. $buffer = fwrite($handle, date("d.M.Y H:i:s") );
  48. fclose ($handle);
  49. }
  50. /**
  51. * remove temp values
  52. */
  53. public function tearDown() {
  54. $this->db->exec("DELETE FROM " . Zend_Registry::get('config')->resources->db->prefix . 'categories');
  55. $this->db->exec("DELETE FROM " . Zend_Registry::get('config')->resources->db->prefix . 'feeds');
  56. $this->db->exec("DELETE FROM " . Zend_Registry::get('config')->resources->db->prefix . 'items');
  57. $this->db->exec("DELETE FROM " . Zend_Registry::get('config')->resources->db->prefix . 'messages');
  58. @unlink(Zend_Registry::get('config')->favicons->path."4711.ico");
  59. @unlink(Zend_Registry::get('config')->favicons->path . md5('http://rsslounge.aditu.de/favicon.ico') . '.ico');
  60. @unlink(Zend_Registry::get('config')->favicons->path . md5('http://blog.aditu.de/favicon.ico') . '.ico');
  61. }
  62. /**
  63. * test configuration
  64. */
  65. public function testConfiguration() {
  66. $this->assertEquals($this->db, $this->model->getAdapter());
  67. $this->assertEquals(array(1 => 'id'), $this->model->info('primary'));
  68. $this->assertEquals(Zend_Registry::get('config')->resources->db->prefix . 'feeds', $this->model->info('name'));
  69. $this->assertEquals(Zend_Registry::get('config')->resources->db->params->dbname, $this->model->info('schema'));
  70. $referenceMap = $this->model->info('referenceMap');
  71. $expected = array(
  72. 'columns' => 'category',
  73. 'refTableClass' => 'application_models_categories',
  74. 'refColumn' => 'id'
  75. );
  76. $this->assertEquals($expected, $referenceMap['categories']);
  77. }
  78. /**
  79. * test max position
  80. * feeds has wrong positions by setUp
  81. */
  82. public function testSort() {
  83. $categoryModel = new application_models_categories();
  84. $this->model->sort($categoryModel->find('2')->current(),array('2','1'));
  85. $this->assertEquals(1, $this->model->find('1')->count());
  86. $this->assertEquals(1, $this->model->find('2')->count());
  87. $this->assertEquals(1, $this->model->find('3')->count());
  88. $this->assertEquals('0', $this->model->find('2')->current()->position); // correct position
  89. $this->assertEquals('2', $this->model->find('2')->current()->category); // corrent category
  90. $this->assertEquals('1', $this->model->find('1')->current()->position); // correct position
  91. $this->assertEquals('2', $this->model->find('1')->current()->category); // corrent category
  92. $this->assertEquals('0', $this->model->find('3')->current()->position); // correct position
  93. $this->assertEquals('1', $this->model->find('3')->current()->category); // corrent category
  94. }
  95. /**
  96. * test counting feeds
  97. */
  98. public function testCount() {
  99. $this->assertEquals(3, $this->model->count(1,2));
  100. $this->assertEquals(2, $this->model->count(1,2,'multimedia'));
  101. $this->assertEquals(1, $this->model->count(1,2,'messages'));
  102. $this->assertEquals(1, $this->model->count(1,1));
  103. $this->assertEquals(2, $this->model->count(2,2));
  104. }
  105. /**
  106. * test reading min and max priority
  107. */
  108. public function testPriority() {
  109. $this->assertEquals(1, $this->model->minPriority());
  110. $this->assertEquals(2, $this->model->maxPriority());
  111. }
  112. /**
  113. * test add new feed
  114. */
  115. public function testAdd() {
  116. $newfeed = array(
  117. 'name' => 'Tobis Blog',
  118. 'url' => 'http://blog.aditu.de/feed',
  119. 'category' => '1',
  120. 'priority' => '3',
  121. 'favicon' => '',
  122. 'filter' => '',
  123. 'source' => 'plugins_rss_feed'
  124. );
  125. $id = $this->model->add($newfeed);
  126. $this->assertTrue(is_numeric($id));
  127. $this->assertGreaterThan(0,$id);
  128. $newfeedEntry = $this->model->find($id);
  129. $this->assertEquals(1,$newfeedEntry->count());
  130. $newfeedEntry = $newfeedEntry->current()->toArray();
  131. $expected = array_merge(
  132. $newfeed,
  133. array(
  134. 'icon' => 'plugins/rss/icon.ico',
  135. 'position' => '3',
  136. 'multimedia' => '0',
  137. 'id' => $id,
  138. 'dirtyicon' => '1',
  139. 'htmlurl' => '',
  140. 'lastrefresh' => null,
  141. 'error' => '0'
  142. )
  143. );
  144. $this->assertEquals($expected, $newfeedEntry);
  145. }
  146. /**
  147. * test edit feed
  148. */
  149. public function testEdit() {
  150. $editfeed = array(
  151. 'id' => '1',
  152. 'name' => 'n-tv all news',
  153. 'url' => 'http://www.n-tv.de/rss',
  154. 'category' => '2',
  155. 'priority' => '1',
  156. 'favicon' => '467321.ico',
  157. 'filter' => '/.*4711.*/',
  158. 'source' => 'plugins_rss_multimedia'
  159. );
  160. $id = $this->model->edit($editfeed);
  161. $this->assertTrue(is_numeric($id));
  162. $this->assertGreaterThan(0,$id);
  163. $newEntry = $this->model->find($id);
  164. $this->assertEquals(1,$newEntry->count());
  165. $newEntry = $newEntry->current()->toArray();
  166. $expected = array_merge(
  167. $editfeed,
  168. array(
  169. 'icon' => 'plugins/rss/multimedia.ico',
  170. 'position' => '0',
  171. 'multimedia' => '1',
  172. 'id' => $id,
  173. 'dirtyicon' => '1',
  174. 'htmlurl' => '',
  175. 'lastrefresh' => null,
  176. 'error' => '0'
  177. )
  178. );
  179. $this->assertEquals($expected, $newEntry);
  180. // check that items was deleted because of the source change
  181. $itemModel = new application_models_items();
  182. $result = $itemModel->fetchAll(
  183. $itemModel->select()->where('feed=?', $id)
  184. );
  185. $this->assertEquals(0, $result->count());
  186. }
  187. /**
  188. * test remove a feed
  189. */
  190. public function testRemove() {
  191. $this->model->remove(3);
  192. // feed really deleted
  193. $this->assertEquals(0, $this->model->find(3)->count());
  194. // all items deleted
  195. $itemModel = new application_models_items();
  196. $result = $itemModel->fetchAll(
  197. $itemModel->select()->where('feed=?', 3)
  198. );
  199. $this->assertEquals(0, $result->count());
  200. // no icon file
  201. $this->assertFalse(file_exists(Zend_Registry::get('config')->favicons->path."4711.ico"));
  202. // no messages
  203. $messagesModel = new application_models_messages();
  204. $result = $messagesModel->fetchAll(
  205. $messagesModel->select()->where('feed=?', 3)
  206. );
  207. $this->assertEquals(0, $result->count());
  208. // check reorder
  209. $this->assertEquals('0', $this->model->find('1')->current()->position); // correct position
  210. $this->assertEquals('1', $this->model->find('2')->current()->position); // correct position
  211. // validation
  212. $this->assertTrue($this->model->remove(222)!==true);
  213. }
  214. /**
  215. * test handling of icons
  216. */
  217. public function testIconhandling() {
  218. // load icon by favicon value
  219. $this->model->saveIcon( $this->model->find(1)->current() );
  220. $this->assertEquals($this->model->find(1)->current()->icon, md5('http://rsslounge.aditu.de/favicon.ico') . '.ico');
  221. // load icon by htmlurl page
  222. $this->model->saveIcon( $this->model->find(2)->current() );
  223. $this->assertEquals($this->model->find(2)->current()->icon, md5('http://blog.aditu.de/wp-content/themes/blog.aditu.de/favicon.ico') . '.ico');
  224. // use plugin icon (invalid url, no htmlurl)
  225. $this->model->saveIcon( $this->model->find(3)->current() );
  226. $this->assertEquals($this->model->find(3)->current()->icon, 'plugins/rss/icon.ico');
  227. // delete icon
  228. $this->model->deleteIcon( $this->model->find(1)->current() );
  229. $this->assertFalse(file_exists(Zend_Registry::get('config')->favicons->path . md5('http://rsslounge.aditu.de/favicon.ico') . '.ico'));
  230. }
  231. /**
  232. * test validation
  233. */
  234. public function testValidation() {
  235. $newfeed = array(
  236. 'name' => 'Tobis Blog',
  237. 'url' => 'http://blog.aditu.de/feed',
  238. 'category' => '1',
  239. 'priority' => '3',
  240. 'favicon' => '',
  241. 'filter' => '',
  242. 'source' => 'plugins_rss_feed'
  243. );
  244. // invalid name
  245. $testfeed = array_merge( $newfeed, array( 'name' => '' ) );
  246. $this->assertTrue(is_array( $this->model->add($testfeed) ));
  247. $testfeed = $newfeed;
  248. unset($testfeed['name']);
  249. $this->assertTrue(is_array( $this->model->add($testfeed) ));
  250. // invalid url
  251. $testfeed = array_merge( $newfeed, array( 'url' => 'http://www.n-tv.de/wirtschaft/rss' ) );
  252. $this->assertTrue(is_array( $this->model->add($testfeed) ));
  253. // url optional => no error
  254. $testfeed = array_merge( $newfeed, array( 'source' => 'plugins_images_visualizeus', 'url' => '' ) );
  255. $id = $this->model->add($testfeed);
  256. $this->assertTrue(is_numeric( $id ));
  257. $this->assertGreaterThan(0, $id);
  258. $this->model->remove($id);
  259. // url not optional => error
  260. $testfeed = array_merge( $newfeed, array( 'url' => '' ) );
  261. $this->assertTrue(is_array( $this->model->add($testfeed) ));
  262. $testfeed = $newfeed;
  263. unset($testfeed['url']);
  264. $this->assertTrue(is_array( $this->model->add($testfeed) ));
  265. // invalid category
  266. $testfeed = array_merge( $newfeed, array( 'category' => 'abc' ) );
  267. $this->assertTrue(is_array( $this->model->add($testfeed) ));
  268. $testfeed = array_merge( $newfeed, array( 'category' => '222' ) );
  269. $this->assertTrue(is_array( $this->model->add($testfeed) ));
  270. // invalid priority
  271. $testfeed = array_merge( $newfeed, array( 'priority' => 'abc' ) );
  272. $this->assertTrue(is_array( $this->model->add($testfeed) ));
  273. $testfeed = $newfeed;
  274. unset($testfeed['priority']);
  275. $this->assertTrue(is_array( $this->model->add($testfeed) ));
  276. // invalid source
  277. $testfeed = $newfeed;
  278. unset($testfeed['source']);
  279. $this->assertTrue(is_array( $this->model->add($testfeed) ));
  280. $testfeed = array_merge( $newfeed, array( 'source' => 'abc' ) );
  281. $this->assertTrue(is_array( $this->model->add($testfeed) ));
  282. // validation of id
  283. $editfeed = array(
  284. 'id' => '1111',
  285. 'name' => 'n-tv all news',
  286. 'url' => 'http://www.n-tv.de/rss',
  287. 'category' => '2',
  288. 'priority' => '1',
  289. 'favicon' => '467321.ico',
  290. 'filter' => '/.*4711.*/',
  291. 'source' => 'plugins_rss_multimedia'
  292. );
  293. $this->assertTrue(is_array( $this->model->edit($editfeed) ));
  294. }
  295. }