Completed
Push — master ( ba8ed9...770316 )
by Jeroen
06:11
created

Tests/unit/Helper/Menu/ActionsMenuBuilderTest.php (3 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 Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Knp\Menu\Integration\Symfony\RoutingExtension;
8
use Knp\Menu\MenuFactory;
9
use Kunstmaan\NodeBundle\Entity\Node;
10
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
11
use Kunstmaan\NodeBundle\Entity\NodeVersion;
12
use Kunstmaan\NodeBundle\Helper\Menu\ActionsMenuBuilder;
13
use Kunstmaan\NodeBundle\Helper\PagesConfiguration;
14
use PHPUnit\Framework\TestCase;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17
use Symfony\Component\Routing\RouterInterface;
18
19
class ActionsMenuBuilderTest extends TestCase
20
{
21
    /**
22
     * @var ActionsMenuBuilder
23
     */
24
    protected $builder;
25
26
    /**
27
     * Sets up the fixture, for example, opens a network connection.
28
     * This method is called before a test is executed.
29
     */
30
    protected function setUp()
31
    {
32
        /* @var UrlGeneratorInterface $urlGenerator */
33
        $urlGenerator = $this->createMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
34
        $routingExtension = new RoutingExtension($urlGenerator);
35
        $factory = new MenuFactory();
36
        $factory->addExtension($routingExtension);
37
        $em = $this->getMockedEntityManager();
38
        /* @var EventDispatcherInterface $dispatcher */
39
        $dispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
40
        /* @var RouterInterface $router */
41
        $router = $this->createMock('Symfony\Component\Routing\RouterInterface');
42
        $authorizationChecker = $this->createMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
43
        $authorizationChecker->expects($this->any())
44
            ->method('isGranted')
45
            ->willReturn(true);
46
47
        $this->builder = new ActionsMenuBuilder($factory, $em, $router, $dispatcher, $authorizationChecker, new PagesConfiguration([]));
48
    }
49
50
    /**
51
     * @throws \Exception
52
     *
53
     * @return \Doctrine\ORM\EntityManager
0 ignored issues
show
Should the return type not be \PHPUnit\Framework\MockObject\MockObject?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

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