Version 1
[yaffs-website] / web / core / modules / forum / tests / src / Unit / ForumManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\forum\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6
7 /**
8  * @coversDefaultClass \Drupal\forum\ForumManager
9  * @group forum
10  */
11 class ForumManagerTest extends UnitTestCase {
12
13   /**
14    * Tests ForumManager::getIndex().
15    */
16   public function testGetIndex() {
17     $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
18
19     $storage = $this->getMockBuilder('\Drupal\taxonomy\VocabularyStorage')
20       ->disableOriginalConstructor()
21       ->getMock();
22
23     $config_factory = $this->getMock('\Drupal\Core\Config\ConfigFactoryInterface');
24
25     $config = $this->getMockBuilder('\Drupal\Core\Config\Config')
26       ->disableOriginalConstructor()
27       ->getMock();
28
29     $config_factory->expects($this->once())
30       ->method('get')
31       ->will($this->returnValue($config));
32
33     $config->expects($this->once())
34       ->method('get')
35       ->will($this->returnValue('forums'));
36
37     $entity_manager->expects($this->once())
38       ->method('getStorage')
39       ->will($this->returnValue($storage));
40
41     // This is sufficient for testing purposes.
42     $term = new \stdClass();
43
44     $storage->expects($this->once())
45       ->method('create')
46       ->will($this->returnValue($term));
47
48     $connection = $this->getMockBuilder('\Drupal\Core\Database\Connection')
49       ->disableOriginalConstructor()
50       ->getMock();
51
52     $translation_manager = $this->getMockBuilder('\Drupal\Core\StringTranslation\TranslationManager')
53       ->disableOriginalConstructor()
54       ->getMock();
55
56     $comment_manager = $this->getMockBuilder('\Drupal\comment\CommentManagerInterface')
57       ->disableOriginalConstructor()
58       ->getMock();
59
60     $manager = $this->getMock('\Drupal\forum\ForumManager', ['getChildren'], [
61       $config_factory,
62       $entity_manager,
63       $connection,
64       $translation_manager,
65       $comment_manager,
66     ]);
67
68     $manager->expects($this->once())
69       ->method('getChildren')
70       ->will($this->returnValue([]));
71
72     // Get the index once.
73     $index1 = $manager->getIndex();
74
75     // Get it again. This should not return the previously generated index. If
76     // it does not, then the test will fail as the mocked methods will be called
77     // more than once.
78     $index2 = $manager->getIndex();
79
80     $this->assertEquals($index1, $index2);
81   }
82
83 }