Completed
Push — master ( 6e92d2...d7f3a5 )
by
unknown
109:07 queued 86:32
created

Tests/Helper/Menu/ActionsMenuBuilderTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeBundle\Tests\Helper\Menu;
4
5
use Knp\Menu\Integration\Symfony\RoutingExtension;
6
use Knp\Menu\MenuFactory;
7
use Kunstmaan\NodeBundle\Entity\Node;
8
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
9
use Kunstmaan\NodeBundle\Entity\NodeVersion;
10
use Kunstmaan\NodeBundle\Helper\Menu\ActionsMenuBuilder;
11
use Kunstmaan\NodeBundle\Helper\PagesConfiguration;
12
use Kunstmaan\NodeBundle\Tests\Stubs\TestRepository;
13
use PHPUnit_Framework_TestCase;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16
use Symfony\Component\Routing\RouterInterface;
17
18
class ActionsMenuBuilderTest extends PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var ActionsMenuBuilder
22
     */
23
    protected $builder;
24
25
    /**
26
     * Sets up the fixture, for example, opens a network connection.
27
     * This method is called before a test is executed.
28
     */
29
    protected function setUp()
30
    {
31
        /* @var UrlGeneratorInterface $urlGenerator */
32
        $urlGenerator = $this->createMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
33
        $routingExtension = new RoutingExtension($urlGenerator);
34
        $factory = new MenuFactory();
35
        $factory->addExtension($routingExtension);
36
        $em = $this->getMockedEntityManager();
37
        /* @var EventDispatcherInterface $dispatcher */
38
        $dispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
39
        /* @var RouterInterface $router */
40
        $router = $this->createMock('Symfony\Component\Routing\RouterInterface');
41
        $authorizationChecker = $this->createMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
42
        $authorizationChecker->expects($this->any())
43
            ->method('isGranted')
44
            ->will($this->returnValue(true));
45
46
        $this->builder = new ActionsMenuBuilder($factory, $em, $router, $dispatcher, $authorizationChecker, new PagesConfiguration([]));
47
    }
48
49
    /**
50
     * https://gist.github.com/1331789
51
     *
52
     * @return \Doctrine\ORM\EntityManager
53
     */
54
    protected function getMockedEntityManager()
55
    {
56
        $emMock = $this->createMock('\Doctrine\ORM\EntityManager',
57
            array('getRepository', 'getClassMetadata', 'persist', 'flush'), array(), '', false);
0 ignored issues
show
The call to ActionsMenuBuilderTest::createMock() has too many arguments starting with array('getRepository', '...a', 'persist', 'flush').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
58
        $emMock->expects($this->any())
59
            ->method('getRepository')
60
            ->will($this->returnValue(new TestRepository()));
61
        $emMock->expects($this->any())
62
            ->method('getClassMetadata')
63
            ->will($this->returnValue((object)array('name' => 'aClass')));
64
        $emMock->expects($this->any())
65
            ->method('persist')
66
            ->will($this->returnValue(null));
67
        $emMock->expects($this->any())
68
            ->method('flush')
69
            ->will($this->returnValue(null));
70
71
        /** @var \Doctrine\ORM\EntityManager $emMock */
72
        return $emMock;  // it tooks 13 lines to achieve mock!
73
    }
74
75 View Code Duplication
    public function testCreateSubActionsMenu()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $nodeTranslation = new NodeTranslation();
78
        $nodeTranslation->setNode(new Node());
79
80
        $nodeVersion = new NodeVersion();
81
        $nodeVersion->setNodeTranslation($nodeTranslation);
82
83
        $this->builder->setActiveNodeVersion($nodeVersion);
84
85
86
        $menu = $this->builder->createSubActionsMenu();
87
        $this->assertNotNull($menu->getChild('subaction.versions'));
88
89
        $this->assertEquals('page-sub-actions', $menu->getChildrenAttribute('class'));
90
    }
91
92
    public function testCreateActionsMenuDraft()
93
    {
94
        $nodeTranslation = new NodeTranslation();
95
        $nodeTranslation->setNode(new Node());
96
97
        $nodeVersion = new NodeVersion();
98
        $nodeVersion->setType('draft');
99
        $nodeVersion->setNodeTranslation($nodeTranslation);
100
101
        $this->builder->setActiveNodeVersion($nodeVersion);
102
103
        $menu = $this->builder->createActionsMenu();
104
        $this->assertNotNull($menu->getChild('action.saveasdraft'));
105
        $this->assertNull($menu->getChild('action.recopyfromlanguage'));
106
        $this->assertNotNull($menu->getChild('action.publish'));
107
        $this->assertNotNull($menu->getChild('action.preview'));
108
        $this->assertNull($menu->getChild('action.save'));
109
110 View Code Duplication
        if ((null !== $nodeTranslation->getNode()->getParent() || $nodeTranslation->getNode()->getChildren()->isEmpty())) {
111
            $this->assertNotNull($menu->getChild('action.delete'));
112
        }
113
        else {
114
            $this->assertNull($menu->getChild('action.delete'));
115
        }
116
117
        $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
118
    }
119
120
    public function testCreateActionsMenuPublic()
121
    {
122
        $nodeTranslation = new NodeTranslation();
123
        $nodeTranslation->setNode(new Node());
124
125
        $nodeVersion = new NodeVersion();
126
        $nodeVersion->setType("public");
127
        $nodeVersion->setNodeTranslation($nodeTranslation);
128
129
        $this->builder->setActiveNodeVersion($nodeVersion);
130
131
132
        $menu = $this->builder->createActionsMenu();
133
        $this->assertNotNull($menu->getChild('action.save'));
134
        $this->assertNotNull($menu->getChild('action.saveasdraft'));
135
        $this->assertNull($menu->getChild('action.recopyfromlanguage'));
136
        $this->assertNotNull($menu->getChild('action.preview'));
137
        $this->assertNotNull($menu->getChild('action.publish'));
138
        $this->assertNull($menu->getChild('action.unpublish'));
139 View Code Duplication
        if ((null !== $nodeTranslation->getNode()->getParent() || $nodeTranslation->getNode()->getChildren()->isEmpty())) {
140
            $this->assertNotNull($menu->getChild('action.delete'));
141
        }
142
        else {
143
            $this->assertNull($menu->getChild('action.delete'));
144
        }
145
146
        $nodeTranslation->setOnline(true);
147
        $menu = $this->builder->createActionsMenu();
148
        $this->assertNotNull($menu->getChild('action.save'));
149
        $this->assertNotNull($menu->getChild('action.saveasdraft'));
150
        $this->assertNull($menu->getChild('action.recopyfromlanguage'));
151
        $this->assertNotNull($menu->getChild('action.preview'));
152
        $this->assertNull($menu->getChild('action.publish'));
153
        $this->assertNotNull($menu->getChild('action.unpublish'));
154 View Code Duplication
        if ((null !== $nodeTranslation->getNode()->getParent() || $nodeTranslation->getNode()->getChildren()->isEmpty())) {
155
            $this->assertNotNull($menu->getChild('action.delete'));
156
        }
157
        else {
158
            $this->assertNull($menu->getChild('action.delete'));
159
        }
160
161
        $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
162
    }
163
164
    public function testCreateActionsMenuNonEditable()
165
    {
166
        $nodeTranslation = new NodeTranslation();
167
        $nodeTranslation->setNode(new Node());
168
169
        $nodeVersion = new NodeVersion();
170
        $nodeVersion->setType("public");
171
        $nodeVersion->setNodeTranslation($nodeTranslation);
172
        $this->builder->setEditableNode(false);
173
174
        $this->builder->setActiveNodeVersion($nodeVersion);
175
        $nodeTranslation->setOnline(false);
176
177
178
        $menu = $this->builder->createActionsMenu();
179
        $this->assertNotNull($menu->getChild('action.save')); // We want to save.
180
        $this->assertNull($menu->getChild('action.saveasdraft'));
181
        $this->assertNull($menu->getChild('action.recopyfromlanguage'));
182
        $this->assertNull($menu->getChild('action.preview'));
183
        $this->assertNull($menu->getChild('action.publish'));
184
        $this->assertNull($menu->getChild('action.unpublish'));
185
186
        $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
187
    }
188
189 View Code Duplication
    public function testCreateTopActionsMenu()
190
    {
191
        $nodeTranslation = new NodeTranslation();
192
        $nodeTranslation->setNode(new Node());
193
194
        $nodeVersion = new NodeVersion();
195
        $nodeVersion->setNodeTranslation($nodeTranslation);
196
197
        $this->builder->setActiveNodeVersion($nodeVersion);
198
199
200
        $menu = $this->builder->createTopActionsMenu();
201
        $this->assertEquals('page-main-actions page-main-actions--top', $menu->getChildrenAttribute('class'));
202
        $this->assertEquals('page-main-actions-top', $menu->getChildrenAttribute('id'));
203
    }
204
205
    public function testSetGetActiveNodeVersion()
206
    {
207
        $nodeVersion = new NodeVersion();
208
        $this->builder->setActiveNodeVersion($nodeVersion);
209
        $this->assertEquals($this->builder->getActiveNodeVersion(), $nodeVersion);
210
    }
211
212
    public function testShouldShowDeleteButtonWhenTheNodeHasAParent()
213
    {
214
        $nodeTranslation = new NodeTranslation();
215
        $node = new Node();
216
        $node->setParent(new Node);
217
        $nodeTranslation->setNode($node);
218
219
        $nodeVersion = new NodeVersion();
220
        $nodeVersion->setType('public');
221
        $nodeVersion->setNodeTranslation($nodeTranslation);
222
223
        $this->builder->setActiveNodeVersion($nodeVersion);
224
225
226
        $menu = $this->builder->createActionsMenu();
227
        $this->assertNotNull($menu->getChild('action.delete'));
228
229
        $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
230
    }
231
232
    public function testShouldShowRecopyButtonWhenTheNodeHasTranslations()
233
    {
234
        $node = new Node();
235
        $nodeTranslation = new NodeTranslation();
236
        $nodeTranslation->setLang('en');
237
238
        $node->addNodeTranslation($nodeTranslation);
239
240
        $nodeVersion = new NodeVersion();
241
        $nodeVersion->setType("public");
242
        $nodeVersion->setNodeTranslation($nodeTranslation);
243
244
        $this->builder->setActiveNodeVersion($nodeVersion);
245
246
        $nodeTranslation = new NodeTranslation();
247
        $nodeTranslation->setLang('nl');
248
249
        $node->addNodeTranslation($nodeTranslation);
250
251
        $nodeVersion = new NodeVersion();
252
        $nodeVersion->setType("public");
253
        $nodeVersion->setNodeTranslation($nodeTranslation);
254
255
        $this->builder->setActiveNodeVersion($nodeVersion);
256
257
        $menu = $this->builder->createActionsMenu();
258
        $this->assertNotNull($menu->getChild('action.recopyfromlanguage'));
259
260
        $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
261
    }
262
}
263