PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/ForumHolderTest.php

https://github.com/lenix/silverstripe-forum
PHP | 159 lines | 81 code | 40 blank | 38 comment | 6 complexity | 79804396d82f63c664af58b5fa22294c MD5 | raw file
  1. <?php
  2. /**
  3. * @todo Write tests to cover the RSS feeds
  4. */
  5. class ForumHolderTest extends FunctionalTest {
  6. static $fixture_file = "forum/tests/ForumTest.yml";
  7. /**
  8. * Tests around multiple forum holders, to ensure that given a forum holder, methods only retrieve
  9. * categories and forums relevant to that holder.
  10. *
  11. * @return unknown_type
  12. */
  13. function testGetForums() {
  14. $fh = $this->objFromFixture("ForumHolder", "fh");
  15. $fh_controller = new ForumHolder_Controller($fh);
  16. // one forum which is viewable.
  17. $this->assertEquals('1', $fh_controller->Forums()->Count(), "Forum holder has 1 forum");
  18. // Test ForumHolder::Categories() on 'fh', from which we expect 2 categories
  19. $this->assertTrue($fh_controller->Categories()->Count() == 2, "fh has two categories");
  20. // Test what we got back from the two categories. The first expects 2 forums, the second
  21. // expects none.
  22. $this->assertTrue($fh_controller->Categories()->First()->Forums()->Count() == 0, "fh first category has 0 forums");
  23. $this->assertTrue($fh_controller->Categories()->Last()->Forums()->Count() == 2, "fh second category has 2 forums");
  24. // Test ForumHolder::Categories() on 'fh2', from which we expect 2 categories
  25. $fh2 = $this->objFromFixture("ForumHolder", "fh2");
  26. $fh2_controller = new ForumHolder_Controller($fh2);
  27. $this->assertTrue($fh2_controller->Categories()->Count() == 2, "fh first forum has two categories");
  28. // Test what we got back from the two categories. Each expects 1.
  29. $this->assertTrue($fh2_controller->Categories()->First()->Forums()->Count() == 1, "fh first category has 1 forums");
  30. $this->assertTrue($fh2_controller->Categories()->Last()->Forums()->Count() == 1, "fh second category has 1 forums");
  31. // plain forums (not nested in categories)
  32. $forumHolder = $this->objFromFixture("ForumHolder", "fhNoCategories");
  33. $this->assertEquals($forumHolder->Forums()->Count(), 1);
  34. $this->assertEquals($forumHolder->Forums()->First()->Title, "Forum Without Category");
  35. // plain forums with nested in categories enabled (but shouldn't effect it)
  36. $forumHolder = $this->objFromFixture("ForumHolder", "fhNoCategories");
  37. $forumHolder->ShowInCategories = true;
  38. $forumHolder->write();
  39. $this->assertEquals($forumHolder->Forums()->Count(), 1);
  40. $this->assertEquals($forumHolder->Forums()->First()->Title, "Forum Without Category");
  41. }
  42. function testGetNumPosts() {
  43. // test holder with posts
  44. $fh = $this->objFromFixture("ForumHolder", "fh");
  45. $this->assertEquals($fh->getNumPosts(), 24);
  46. // test holder that doesn't have posts
  47. $fh2 = $this->objFromFixture("ForumHolder", "fh2");
  48. $this->assertEquals($fh2->getNumPosts(), 0);
  49. }
  50. function testGetNumTopics() {
  51. // test holder with posts
  52. $fh = $this->objFromFixture("ForumHolder", "fh");
  53. $this->assertEquals($fh->getNumTopics(), 6);
  54. // test holder that doesn't have posts
  55. $fh2 = $this->objFromFixture("ForumHolder", "fh2");
  56. $this->assertEquals($fh2->getNumTopics(), 0);
  57. }
  58. function testGetNumAuthors() {
  59. // test holder with posts
  60. $fh = $this->objFromFixture("ForumHolder", "fh");
  61. $this->assertEquals($fh->getNumAuthors(), 4);
  62. // test holder that doesn't have posts
  63. $fh2 = $this->objFromFixture("ForumHolder", "fh2");
  64. $this->assertEquals($fh2->getNumAuthors(), 0);
  65. }
  66. function testGetRecentPosts() {
  67. // test holder with posts
  68. $fh = $this->objFromFixture("ForumHolder", "fh");
  69. // make sure all the posts are included
  70. $this->assertEquals($fh->getRecentPosts()->Count(), 24);
  71. // check they're in the right order (well if the first and last are right its fairly safe)
  72. $this->assertEquals($fh->getRecentPosts()->First()->Content, "This is the last post to a long thread");
  73. // test holder that doesn't have posts
  74. $fh2 = $this->objFromFixture("ForumHolder", "fh2");
  75. $this->assertNull($fh2->getRecentPosts());
  76. // test trying to get recent posts specific forum without posts
  77. $forum = $this->objFromFixture("Forum", "forum1cat2");
  78. $this->assertNull($fh->getRecentPosts(50, $forum->ID));
  79. // test trying to get recent posts specific to a forum which has posts
  80. $forum = $this->objFromFixture("Forum", "general");
  81. $this->assertEquals($fh->getRecentPosts(50, $forum->ID)->Count(), 24);
  82. $this->assertEquals($fh->getRecentPosts(50, $forum->ID)->First()->Content, "This is the last post to a long thread");
  83. // test trying to filter by a specific thread
  84. $thread = $this->objFromFixture("ForumThread","Thread1");
  85. $this->assertEquals($fh->getRecentPosts(50, null, $thread->ID)->Count(), 17);
  86. $this->assertEquals($fh->getRecentPosts(10, null, $thread->ID)->Count(), 10);
  87. $this->assertEquals($fh->getRecentPosts(50, null, $thread->ID)->First()->Content, 'This is the last post to a long thread');
  88. // test limiting the response
  89. $this->assertEquals($fh->getRecentPosts(1)->Count(), 1);
  90. }
  91. function testGlobalAnnouncements() {
  92. // test holder with posts
  93. $fh = $this->objFromFixture("ForumHolder", "fh");
  94. $controller = new ForumHolder_Controller($fh);
  95. // make sure all the announcements are included
  96. $this->assertEquals($controller->GlobalAnnouncements()->Count(), 1);
  97. // test holder that doesn't have posts
  98. $fh2 = $this->objFromFixture("ForumHolder", "fh2");
  99. $controller2 = new ForumHolder_Controller($fh2);
  100. $this->assertEquals($controller2->GlobalAnnouncements()->Count(), 0);
  101. }
  102. function testGetNewPostsAvailable() {
  103. $fh = $this->objFromFixture("ForumHolder", "fh");
  104. // test last visit. we can assume that these tests have been reloaded in the past 24 hours
  105. $this->assertTrue($fh->getNewPostsAvailable(date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), date('d')-1, date('Y')))));
  106. // set the last post ID (test the first post - so there should be a post, last post (false))
  107. $lastPostID = array_pop($this->allFixtureIDs('Post'));
  108. $this->assertTrue($fh->getNewPostsAvailable(null, 1));
  109. $this->assertFalse($fh->getNewPostsAvailable(null, $lastPostID));
  110. // limit to a specific forum
  111. $forum = $this->objFromFixture("Forum", "general");
  112. $this->assertTrue($fh->getNewPostsAvailable(null, null, $forum->ID));
  113. $this->assertFalse($fh->getNewPostsAvailable(null, $lastPostID, $forum->ID));
  114. // limit to a specific thread
  115. $thread = $this->objFromFixture("ForumThread", "Thread1");
  116. $this->assertTrue($fh->getNewPostsAvailable(null, null, null, $thread->ID));
  117. $this->assertFalse($fh->getNewPostsAvailable(null, $lastPostID, null, $thread->ID));
  118. }
  119. }