Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* @file
* Contains \Drupal\forum\Tests\Breadcrumb\ForumListingBreadcrumbBuilderTest.
*/
namespace Drupal\forum\Tests\Breadcrumb;
use Drupal\Tests\UnitTestCase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
/**
* Tests the listing class for forum breadcrumbs.
*
* @coversDefaultClass \Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder
* @group Forum
* @group Drupal
*
* @see \Drupal\forum\ForumListingBreadcrumbBuilder
*/
class ForumListingBreadcrumbBuilderTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Forum Breadcrumb Listing Test',
'description' => 'Tests the listing class for forum breadcrumbs.',
'group' => 'Forum',
);
}
/**
* Tests ForumListingBreadcrumbBuilder::applies().
*
* @param bool $expected
* ForumListingBreadcrumbBuilder::applies() expected result.
* @param array $attributes
* ForumListingBreadcrumbBuilder::applies() $attributes parameter.
*
* @dataProvider providerTestApplies
* @covers ::applies
*/
public function testApplies($expected, $attributes) {
// Make some test doubles.
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$config_factory = $this->getConfigFactoryStub(array());
$forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
// Make an object to test.
$builder = $this->getMockBuilder('Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder')
->setConstructorArgs(array(
$entity_manager,
$config_factory,
$forum_manager,
))
->setMethods(NULL)
->getMock();
$this->assertEquals($expected, $builder->applies($attributes));
}
/**
* Provides test data for testApplies().
*
* @return array
* Array of datasets for testApplies(). Structured as such:
* - ForumListBreadcrumbBuilder::applies() expected result.
* - ForumListBreadcrumbBuilder::applies() $attributes input array.
*/
public function providerTestApplies() {
// Send a Node mock, because NodeInterface cannot be mocked.
$mock_term = $this->getMockBuilder('Drupal\taxonomy\Entity\Term')
->disableOriginalConstructor()
->getMock();
return array(
array(
FALSE,
array(),
),
array(
FALSE,
array(
RouteObjectInterface::ROUTE_NAME => 'NOT.forum.page',
),
),
array(
FALSE,
array(
RouteObjectInterface::ROUTE_NAME => 'forum.page',
),
),
array(
TRUE,
array(
RouteObjectInterface::ROUTE_NAME => 'forum.page',
'taxonomy_term' => 'anything',
),
),
array(
TRUE,
array(
RouteObjectInterface::ROUTE_NAME => 'forum.page',
'taxonomy_term' => $mock_term,
),
),
);
}
/**
* Tests ForumListingBreadcrumbBuilder::build().
*
* @see \Drupal\forum\ForumListingBreadcrumbBuilder::build()
*
* @covers ::build
*/
public function testBuild() {
// Build all our dependencies, backwards.
$term1 = $this->getMockBuilder('Drupal\taxonomy\Entity\Term')
->disableOriginalConstructor()
->getMock();
$term1->expects($this->any())
->method('label')
->will($this->returnValue('Something'));
$term1->expects($this->any())
->method('id')
->will($this->returnValue(1));
$term2 = $this->getMockBuilder('Drupal\taxonomy\Entity\Term')
->disableOriginalConstructor()
->getMock();
$term2->expects($this->any())
->method('label')
->will($this->returnValue('Something else'));
$term2->expects($this->any())
->method('id')
->will($this->returnValue(2));
$forum_manager = $this->getMock('Drupal\forum\ForumManagerInterface');
$forum_manager->expects($this->at(0))
->method('getParents')
->will($this->returnValue(array($term1)));
$forum_manager->expects($this->at(1))
->method('getParents')
->will($this->returnValue(array($term1, $term2)));
// The root forum.
$vocab_item = $this->getMock('Drupal\taxonomy\VocabularyInterface');
$vocab_item->expects($this->any())
->method('label')
->will($this->returnValue('Fora_is_the_plural_of_forum'));
catch
committed
$vocab_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
$vocab_storage->expects($this->any())
->method('load')
->will($this->returnValueMap(array(
array('forums', $vocab_item),
)));
$entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManagerInterface')
->disableOriginalConstructor()
->getMock();
$entity_manager->expects($this->any())
catch
committed
->method('getStorage')
->will($this->returnValueMap(array(
catch
committed
array('taxonomy_vocabulary', $vocab_storage),
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
)));
$config_factory = $this->getConfigFactoryStub(
array(
'forum.settings' => array(
'vocabulary' => 'forums',
),
)
);
// Build a breadcrumb builder to test.
$breadcrumb_builder = $this->getMock(
'Drupal\forum\Breadcrumb\ForumListingBreadcrumbBuilder', NULL, array(
$entity_manager,
$config_factory,
$forum_manager,
)
);
// Add a translation manager for t().
$translation_manager = $this->getStringTranslationStub();
$property = new \ReflectionProperty('Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder', 'translationManager');
$property->setAccessible(TRUE);
$property->setValue($breadcrumb_builder, $translation_manager);
// Add a link generator for l().
$link_generator = $this->getMockBuilder('Drupal\Core\Utility\LinkGeneratorInterface')
->disableOriginalConstructor()
->getMock();
$link_generator->expects($this->any())
->method('generate')
->will($this->returnArgument(0));
$property = new \ReflectionProperty('Drupal\forum\Breadcrumb\ForumNodeBreadcrumbBuilder', 'linkGenerator');
$property->setAccessible(TRUE);
$property->setValue($breadcrumb_builder, $link_generator);
// The forum listing we need a breadcrumb back from.
$forum_listing = $this->getMockBuilder('Drupal\taxonomy\Entity\Term')
->disableOriginalConstructor()
->getMock();
$forum_listing->tid = 23;
$forum_listing->expects($this->any())
->method('label')
->will($this->returnValue('You_should_not_see_this'));
// Our data set.
$attributes = array(
'taxonomy_term' => $forum_listing,
);
// First test.
$expected1 = array(
'Home',
'Fora_is_the_plural_of_forum',
'Something',
);
$this->assertSame($expected1, $breadcrumb_builder->build($attributes));
// Second test.
$expected2 = array(
'Home',
'Fora_is_the_plural_of_forum',
'Something else',
'Something',
);
$this->assertSame($expected2, $breadcrumb_builder->build($attributes));
}
}