/content/test/Content/Base.php

https://github.com/sgtcarneiro/horde · PHP · 386 lines · 260 code · 52 blank · 74 comment · 3 complexity · a0abb3a9bd3b736f0893ce6644ca6a83 MD5 · raw file

  1. <?php
  2. require_once dirname(__FILE__) . '/Autoload.php';
  3. /**
  4. * Copyright 2008-2011 The Horde Project (http://www.horde.org/)
  5. *
  6. * @author Michael J Rubinsky <mrubinsk@horde.org>
  7. * @author Chuck Hagenbuch <chuck@horde.org>
  8. * @category Horde
  9. * @package Content
  10. * @subpackage UnitTests
  11. */
  12. class Content_Test_Base extends Horde_Test_Case
  13. {
  14. /**
  15. * @static Content_Tagger
  16. */
  17. static $tagger;
  18. /**
  19. * Cache the tag_id => tag_names for further testing
  20. *
  21. * @var array
  22. */
  23. protected $_tags;
  24. /**
  25. * Primes the fixture, and tests basic tagging functionality where all
  26. * bits of data are new (user, type, object, tag)..
  27. *
  28. */
  29. protected function _create()
  30. {
  31. $this->_testEmpty();
  32. // user alice tags an event named 'party' with the tag 'personal' and
  33. // an event named 'anniversary' with the tag 'personal'
  34. self::$tagger->tag('alice', array('type' => 'event', 'object' => 'party'), 'play', new Horde_Date('2008-01-01T00:10:00'));
  35. // user alice tags an event named 'office hours' with the tag 'work'
  36. self::$tagger->tag('alice', array('type' => 'event', 'object' => 'office hours'), 'work', new Horde_Date('2008-01-01T00:05:00'));
  37. // user bob tags a blog named 'daring fireball' with the tag 'apple'
  38. self::$tagger->tag('bob', array('type' => 'blog', 'object' => 'daring fireball'), 'apple', new Horde_Date('2008-01-01T00:20:00'));
  39. // Two users have tagged the same object, with the same tag
  40. self::$tagger->tag('alice', array('type' => 'event', 'object' => 'anniversary'), 'personal', new Horde_Date('2009-01-01T00:05:00'));
  41. self::$tagger->tag('bob', array('type' => 'event', 'object' => 'anniversary'), 'personal', new Horde_Date('2009-01-01T00:06:00'));
  42. // save for later.
  43. $this->_tags = self::$tagger->getTags(array());
  44. // Check that the tags now exist.
  45. //$this->assertEquals(4, count(self::$tagger->getTags(array())));
  46. }
  47. protected function _testEmpty()
  48. {
  49. // Basic check that no data exists.
  50. $this->assertEmpty(self::$tagger->getTags(array()));
  51. $this->assertEmpty(self::$tagger->getRecentTags());
  52. $this->assertEmpty(self::$tagger->getRecentObjects());
  53. }
  54. /**
  55. * Test ensureTags.
  56. *
  57. * 1 => play
  58. * 2 => work
  59. * 3 => apple
  60. * 4 => personal
  61. */
  62. protected function _testEnsureTags()
  63. {
  64. // Test passing tag_ids to ensureTags
  65. $this->assertEquals(array(1), self::$tagger->ensureTags(1));
  66. $this->assertEquals(array(1), self::$tagger->ensureTags(array(1)));
  67. $this->assertEquals(array(1, 2), self::$tagger->ensureTags(array(1, 2)));
  68. // Test passing tag names
  69. $this->assertEquals(array(2), self::$tagger->ensureTags('work'));
  70. $this->assertEquals(array(2), self::$tagger->ensureTags(array('work')));
  71. $this->assertEquals(array(2, 1), self::$tagger->ensureTags(array('work', 'play')));
  72. // Test mixed
  73. $this->assertEquals(array(1, 1), self::$tagger->ensureTags(array(1, 'play')));
  74. $this->assertEquals(array(2, 2), self::$tagger->ensureTags(array('work', 2)));
  75. $this->assertEquals(array(1, 2), self::$tagger->ensureTags(array(1, 'work')));
  76. }
  77. protected function _testFullTagCloudSimple()
  78. {
  79. $expected = array(
  80. '1' => array(
  81. 'tag_id' => 1,
  82. 'tag_name' => 'play',
  83. 'count' => 1
  84. ),
  85. '2' => array(
  86. 'tag_id' => 2,
  87. 'tag_name' => 'work',
  88. 'count' => 1
  89. ),
  90. '3' => array(
  91. 'tag_id' => 3,
  92. 'tag_name' => 'apple',
  93. 'count' => 1
  94. ),
  95. '4' => array(
  96. 'tag_id' => 4,
  97. 'tag_name' => 'personal',
  98. 'count' => 2
  99. )
  100. );
  101. $cloud = self::$tagger->getTagCloud();
  102. $this->assertEquals($expected, $cloud);
  103. }
  104. protected function _testTagCloudByType()
  105. {
  106. $expected = array(
  107. '3' => array(
  108. 'tag_id' => 3,
  109. 'tag_name' => 'apple',
  110. 'count' => 1
  111. )
  112. );
  113. $cloud = self::$tagger->getTagCloud(array('typeId' => 'blog'));
  114. $this->assertEquals($expected, $cloud);
  115. }
  116. protected function _testTagCloudByUser()
  117. {
  118. $expected = array(
  119. '3' => array(
  120. 'tag_id' => 3,
  121. 'tag_name' => 'apple',
  122. 'count' => 1
  123. ),
  124. '4' => array(
  125. 'tag_id' => 4,
  126. 'tag_name' => 'personal',
  127. 'count' => 1
  128. )
  129. );
  130. $cloud = self::$tagger->getTagCloud(array('userId' => 'bob'));
  131. $this->assertEquals($expected, $cloud);
  132. }
  133. protected function _testTagCloudByUserType()
  134. {
  135. $expected = array(
  136. '1' => array(
  137. 'tag_id' => 1,
  138. 'tag_name' => 'play',
  139. 'count' => 1
  140. ),
  141. '2' => array(
  142. 'tag_id' => 2,
  143. 'tag_name' => 'work',
  144. 'count' => 1
  145. ),
  146. '4' => array(
  147. 'tag_id' => 4,
  148. 'tag_name' => 'personal',
  149. 'count' => 1
  150. )
  151. );
  152. $cloud = self::$tagger->getTagCloud(array('userId' => 'alice', 'typeId' => 'event'));
  153. $this->assertEquals($expected, $cloud);
  154. }
  155. protected function _testTagCloudByTagType()
  156. {
  157. $expected = array(
  158. '2' => array(
  159. 'tag_id' => 2,
  160. 'tag_name' => 'work',
  161. 'count' => 1
  162. )
  163. );
  164. $cloud = self::$tagger->getTagCloud(array('tagIds' => array(2), 'typeId' => 'event'));
  165. $this->assertEquals($expected, $cloud);
  166. }
  167. protected function _testTagCloudByTagIds()
  168. {
  169. $expected = array(
  170. '2' => array(
  171. 'tag_id' => 2,
  172. 'tag_name' => 'work',
  173. 'count' => 1
  174. ),
  175. '4' => array(
  176. 'tag_id' => 4,
  177. 'tag_name' => 'personal',
  178. 'count' => 2
  179. )
  180. );
  181. $cloud = self::$tagger->getTagCloud(array('tagIds' => array(2, 4)));
  182. $this->assertEquals($expected, $cloud);
  183. }
  184. protected function _testGetRecentTags()
  185. {
  186. $recent = self::$tagger->getRecentTags();
  187. $this->assertEquals(4, count($recent));
  188. $this->assertEquals(4, $recent[0]['tag_id']);
  189. $this->assertEquals('personal', $recent[0]['tag_name']);
  190. $this->assertEquals('2009-01-01 00:06:00', $recent[0]['created']);
  191. }
  192. protected function _testGetRecentTagsByUser()
  193. {
  194. $recent = self::$tagger->getRecentTags(array('userId' => 1));
  195. $this->assertEquals(3, count($recent));
  196. $recent = self::$tagger->getRecentTags(array('userId' => 2));
  197. $this->assertEquals(2, count($recent));
  198. $recent = self::$tagger->getRecentTags(array('userId' => 'alice'));
  199. $this->assertEquals(3, count($recent));
  200. }
  201. protected function _testGetRecentTagsByType()
  202. {
  203. $recent = self::$tagger->getRecentTags(array('typeId' => 'event'));
  204. $this->assertEquals(3, count($recent));
  205. }
  206. protected function _testGetRecentObjects()
  207. {
  208. $recent = self::$tagger->getRecentObjects();
  209. $this->assertEquals(4, count($recent));
  210. $this->assertEquals(4, $recent[0]['object_id']);
  211. $this->assertEquals('2009-01-01 00:06:00', $recent[0]['created']);
  212. }
  213. protected function _testUntag()
  214. {
  215. self::$tagger->untag('alice', array('type' => 'event', 'object' => 'party'), 'play');
  216. $count = self::$tagger->getRecentTags();
  217. $this->assertEquals(3, count($count));
  218. //readd
  219. self::$tagger->tag('alice', array('type' => 'event', 'object' => 'party'), 'play', new Horde_Date('2008-01-01T00:10:00'));
  220. $count = self::$tagger->getRecentTags();
  221. $this->assertEquals(4, count($count));
  222. }
  223. /**
  224. * @TODO: SHould validate the values too, not just the count.
  225. */
  226. protected function _testGetRecentObjectsByUser()
  227. {
  228. // alice has 3 recent objects
  229. $recent = self::$tagger->getRecentObjects(array('userId' => 'alice'));
  230. $this->assertEquals(3, count($recent));
  231. // bob has 2
  232. $recent = self::$tagger->getRecentObjects(array('userId' => 'bob'));
  233. $this->assertEquals(2, count($recent));
  234. // just for kicks, test using the user id, not name.
  235. $recent = self::$tagger->getRecentObjects(array('userId' => 1));
  236. $this->assertEquals(3, count($recent));
  237. }
  238. protected function _testGetRecentObjectsByType()
  239. {
  240. $recent = self::$tagger->getRecentObjects(array('typeId' => 1));
  241. $this->assertEquals(3, count($recent));
  242. $recent = self::$tagger->getRecentObjects(array('typeId' => 2));
  243. $this->assertEquals(1, count($recent));
  244. }
  245. protected function _testGetRecentUsers()
  246. {
  247. $recent = self::$tagger->getRecentUsers();
  248. $this->assertEquals(2, count($recent));
  249. }
  250. protected function _testGetRecentUsersByType()
  251. {
  252. $recent = self::$tagger->getRecentUsers(array('typeId' => 1));
  253. $this->assertEquals(2, count($recent));
  254. $recent = self::$tagger->getRecentUsers(array('typeId' => 2));
  255. $this->assertEquals(1, count($recent));
  256. }
  257. public function testDuplicateTagsByCase()
  258. {
  259. // These tests don't work at the moment, because SQLite sucks at
  260. // non-ascii comparing.
  261. /*
  262. self::$tagger->tag('mike', 1, 'TYÖ');
  263. self::$tagger->tag('mike', 1, 'TYÖ');
  264. self::$tagger->tag('mike', 1, 'työ');
  265. self::$tagger->tag('mike', 1, 'työ');
  266. */
  267. // Use older timestamps to avoid interfering with the later tests
  268. self::$tagger->tag('mike', array('type' => 'foo', 'object' => 'xyz'), 'foo', new Horde_Date('2008-01-01T00:05:00'));
  269. self::$tagger->tag('alice', array('type' => 'foo', 'object' => 'xyz'), 'FOO', new Horde_Date('2008-01-01T00:05:00'));
  270. self::$tagger->tag('alice', array('type' => 'foo', 'object' => 'xyz'), array('test', 'TEST'), new Horde_Date('2008-01-01T00:05:00'));
  271. $this->assertEquals(2, count(self::$tagger->getTags(array('objectId' => array('type' => 'foo', 'object' => 'xyz')))));
  272. }
  273. public function testGetRecentTagsLimit()
  274. {
  275. // Create 100 tags on 100 tag_ids, with tag_id = t1 being applied
  276. // most recently, and so on. Prepend "t" to each tag to force the
  277. // creation of tags that don't yet exist in the test database.
  278. for ($i = 1; $i <= 100; $i++) {
  279. self::$tagger->tag(1, 1, "t$i", new Horde_Date(strtotime('now - ' . $i . ' minutes')));
  280. }
  281. $recentLimit = self::$tagger->getRecentTags(array('limit' => 25));
  282. $this->assertEquals(25, count($recentLimit));
  283. $this->assertEquals('t1', $recentLimit[0]['tag_name']);
  284. }
  285. /**
  286. * @depends testGetRecentTagsLimit
  287. */
  288. public function testGetRecentTagsOffset()
  289. {
  290. $recentOffset = self::$tagger->getRecentTags(array('limit' => 25, 'offset' => 25));
  291. $this->assertEquals(25, count($recentOffset));
  292. $this->assertEquals('t26', $recentOffset[0]['tag_name']);
  293. }
  294. public function testGetRecentObjectsLimit()
  295. {
  296. // Create 100 tags on 100 object_ids, with object_id = 1 being tagged
  297. // most recently, and so on.
  298. for ($i = 1; $i <= 100; $i++) {
  299. self::$tagger->tag(1, $i, 1, new Horde_Date(strtotime('now - ' . $i . ' minutes')));
  300. }
  301. $recentLimit = self::$tagger->getRecentObjects(array('limit' => 25));
  302. $this->assertEquals(25, count($recentLimit));
  303. $this->assertEquals(1, $recentLimit[0]['object_id']);
  304. }
  305. /**
  306. * @depend testGetRecentTagsOffset
  307. */
  308. public function testGetRecentObjectsOffset()
  309. {
  310. $recentOffset = self::$tagger->getRecentObjects(array('limit' => 25, 'offset' => 25));
  311. $this->assertEquals(25, count($recentOffset));
  312. $this->assertEquals(26, $recentOffset[0]['object_id']);
  313. }
  314. public function testGetRecentUsersLimit()
  315. {
  316. // Create 100 tags by 100 user_ids, with user_id = 1 tagging
  317. // most recently, and so on.
  318. for ($i = 1; $i <= 100; $i++) {
  319. self::$tagger->tag($i, 1, 1, new Horde_Date(strtotime('now - ' . $i . ' minutes')));
  320. }
  321. $recentLimit = self::$tagger->getRecentUsers(array('limit' => 25));
  322. $this->assertEquals(25, count($recentLimit));
  323. $this->assertEquals(1, $recentLimit[0]['user_id']);
  324. }
  325. /**
  326. * @depend testGetRecentUsersLimit
  327. */
  328. public function testGetRecentUsersOffset()
  329. {
  330. $recentOffset = self::$tagger->getRecentUsers(array('limit' => 25, 'offset' => 25));
  331. $this->assertEquals(25, count($recentOffset));
  332. $this->assertEquals(26, $recentOffset[0]['user_id']);
  333. }
  334. }