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

Tests/unit/Helper/Menu/ActionsMenuBuilderTest.php (1 issue)

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 Codeception\Stub;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\EntityRepository;
8
use Knp\Menu\Integration\Symfony\RoutingExtension;
9
use Knp\Menu\MenuFactory;
10
use Kunstmaan\NodeBundle\Entity\Node;
11
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
12
use Kunstmaan\NodeBundle\Entity\NodeVersion;
13
use Kunstmaan\NodeBundle\Helper\Menu\ActionsMenuBuilder;
14
use Kunstmaan\NodeBundle\Helper\PagesConfiguration;
15
use PHPUnit_Framework_TestCase;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18
use Symfony\Component\Routing\RouterInterface;
19
20
class ActionsMenuBuilderTest extends PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var ActionsMenuBuilder
24
     */
25
    protected $builder;
26
27
    /**
28
     * Sets up the fixture, for example, opens a network connection.
29
     * This method is called before a test is executed.
30
     */
31
    protected function setUp()
32
    {
33
        /* @var UrlGeneratorInterface $urlGenerator */
34
        $urlGenerator = $this->createMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
35
        $routingExtension = new RoutingExtension($urlGenerator);
36
        $factory = new MenuFactory();
37
        $factory->addExtension($routingExtension);
38
        $em = $this->getMockedEntityManager();
39
        /* @var EventDispatcherInterface $dispatcher */
40
        $dispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
41
        /* @var RouterInterface $router */
42
        $router = $this->createMock('Symfony\Component\Routing\RouterInterface');
43
        $authorizationChecker = $this->createMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
44
        $authorizationChecker->expects($this->any())
45
            ->method('isGranted')
46
            ->will($this->returnValue(true));
47
48
        $this->builder = new ActionsMenuBuilder($factory, $em, $router, $dispatcher, $authorizationChecker, new PagesConfiguration([]));
49
    }
50
51
    /**
52
     * @throws \Exception
53
     *
54
     * @return \Doctrine\ORM\EntityManager
55
     */
56
    protected function getMockedEntityManager()
57
    {
58
        $repository = Stub::make(EntityRepository::class, [
59
            'find' => null,
60
            'findBy' => null,
61
            'findOneBy' => null,
62
        ]);
63
        /** @var \Doctrine\ORM\EntityManager $emMock */
64
        $emMock = Stub::make(EntityManager::class, [
65
            'getRepository' => $repository,
66
            'getClassMetaData' => (object)['name' => 'aClass'],
67
            'persist' => null,
68
            'flush' => null
69
        ]);
70
71
        return $emMock;
72
    }
73
74 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...
75
    {
76
        $nodeTranslation = new NodeTranslation();
77
        $nodeTranslation->setNode(new Node());
78
79
        $nodeVersion = new NodeVersion();
80
        $nodeVersion->setNodeTranslation($nodeTranslation);
81
82
        $this->builder->setActiveNodeVersion($nodeVersion);
83
84
85
        $menu = $this->builder->createSubActionsMenu();
86
        $this->assertNotNull($menu->getChild('subaction.versions'));
87
88
        $this->assertEquals('page-sub-actions', $menu->getChildrenAttribute('class'));
89
    }
90
91
    public function testCreateActionsMenuDraft()
92
    {
93
        $nodeTranslation = new NodeTranslation();
94
        $nodeTranslation->setNode(new Node());
95
96
        $nodeVersion = new NodeVersion();
97
        $nodeVersion->setType('draft');
98
        $nodeVersion->setNodeTranslation($nodeTranslation);
99
100
        $this->builder->setActiveNodeVersion($nodeVersion);
101
102
        $menu = $this->builder->createActionsMenu();
103
        $this->assertNotNull($menu->getChild('action.saveasdraft'));
104
        $this->assertNull($menu->getChild('action.recopyfromlanguage'));
105
        $this->assertNotNull($menu->getChild('action.publish'));
106
        $this->assertNotNull($menu->getChild('action.preview'));
107
        $this->assertNull($menu->getChild('action.save'));
108
109 View Code Duplication
        if ((null !== $nodeTranslation->getNode()->getParent() || $nodeTranslation->getNode()->getChildren()->isEmpty())) {
110
            $this->assertNotNull($menu->getChild('action.delete'));
111
        }
112
        else {
113
            $this->assertNull($menu->getChild('action.delete'));
114
        }
115
116
        $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
117
    }
118
119
    public function testCreateActionsMenuPublic()
120
    {
121
        $nodeTranslation = new NodeTranslation();
122
        $nodeTranslation->setNode(new Node());
123
124
        $nodeVersion = new NodeVersion();
125
        $nodeVersion->setType("public");
126
        $nodeVersion->setNodeTranslation($nodeTranslation);
127
128
        $this->builder->setActiveNodeVersion($nodeVersion);
129
130
131
        $menu = $this->builder->createActionsMenu();
132
        $this->assertNotNull($menu->getChild('action.save'));
133
        $this->assertNotNull($menu->getChild('action.saveasdraft'));
134
        $this->assertNull($menu->getChild('action.recopyfromlanguage'));
135
        $this->assertNotNull($menu->getChild('action.preview'));
136
        $this->assertNotNull($menu->getChild('action.publish'));
137
        $this->assertNull($menu->getChild('action.unpublish'));
138 View Code Duplication
        if ((null !== $nodeTranslation->getNode()->getParent() || $nodeTranslation->getNode()->getChildren()->isEmpty())) {
139
            $this->assertNotNull($menu->getChild('action.delete'));
140
        }
141
        else {
142
            $this->assertNull($menu->getChild('action.delete'));
143
        }
144
145
        $nodeTranslation->setOnline(true);
146
        $menu = $this->builder->createActionsMenu();
147
        $this->assertNotNull($menu->getChild('action.save'));
148
        $this->assertNotNull($menu->getChild('action.saveasdraft'));
149
        $this->assertNull($menu->getChild('action.recopyfromlanguage'));
150
        $this->assertNotNull($menu->getChild('action.preview'));
151
        $this->assertNull($menu->getChild('action.publish'));
152
        $this->assertNotNull($menu->getChild('action.unpublish'));
153 View Code Duplication
        if ((null !== $nodeTranslation->getNode()->getParent() || $nodeTranslation->getNode()->getChildren()->isEmpty())) {
154
            $this->assertNotNull($menu->getChild('action.delete'));
155
        }
156
        else {
157
            $this->assertNull($menu->getChild('action.delete'));
158
        }
159
160
        $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
161
    }
162
163
    public function testCreateActionsMenuNonEditable()
164
    {
165
        $nodeTranslation = new NodeTranslation();
166
        $nodeTranslation->setNode(new Node());
167
168
        $nodeVersion = new NodeVersion();
169
        $nodeVersion->setType("public");
170
        $nodeVersion->setNodeTranslation($nodeTranslation);
171
        $this->builder->setEditableNode(false);
172
173
        $this->builder->setActiveNodeVersion($nodeVersion);
174
        $nodeTranslation->setOnline(false);
175
176
177
        $menu = $this->builder->createActionsMenu();
178
        $this->assertNotNull($menu->getChild('action.save')); // We want to save.
179
        $this->assertNull($menu->getChild('action.saveasdraft'));
180
        $this->assertNull($menu->getChild('action.recopyfromlanguage'));
181
        $this->assertNull($menu->getChild('action.preview'));
182
        $this->assertNull($menu->getChild('action.publish'));
183
        $this->assertNull($menu->getChild('action.unpublish'));
184
185
        $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
186
    }
187
188 View Code Duplication
    public function testCreateTopActionsMenu()
189
    {
190
        $nodeTranslation = new NodeTranslation();
191
        $nodeTranslation->setNode(new Node());
192
193
        $nodeVersion = new NodeVersion();
194
        $nodeVersion->setNodeTranslation($nodeTranslation);
195
196
        $this->builder->setActiveNodeVersion($nodeVersion);
197
198
199
        $menu = $this->builder->createTopActionsMenu();
200
        $this->assertEquals('page-main-actions page-main-actions--top', $menu->getChildrenAttribute('class'));
201
        $this->assertEquals('page-main-actions-top', $menu->getChildrenAttribute('id'));
202
    }
203
204
    public function testSetGetActiveNodeVersion()
205
    {
206
        $nodeVersion = new NodeVersion();
207
        $this->builder->setActiveNodeVersion($nodeVersion);
208
        $this->assertEquals($this->builder->getActiveNodeVersion(), $nodeVersion);
209
    }
210
211
    public function testShouldShowDeleteButtonWhenTheNodeHasAParent()
212
    {
213
        $nodeTranslation = new NodeTranslation();
214
        $node = new Node();
215
        $node->setParent(new Node);
216
        $nodeTranslation->setNode($node);
217
218
        $nodeVersion = new NodeVersion();
219
        $nodeVersion->setType('public');
220
        $nodeVersion->setNodeTranslation($nodeTranslation);
221
222
        $this->builder->setActiveNodeVersion($nodeVersion);
223
224
225
        $menu = $this->builder->createActionsMenu();
226
        $this->assertNotNull($menu->getChild('action.delete'));
227
228
        $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
229
    }
230
231
    public function testShouldShowRecopyButtonWhenTheNodeHasTranslations()
232
    {
233
        $node = new Node();
234
        $nodeTranslation = new NodeTranslation();
235
        $nodeTranslation->setLang('en');
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
        $nodeTranslation = new NodeTranslation();
246
        $nodeTranslation->setLang('nl');
247
248
        $node->addNodeTranslation($nodeTranslation);
249
250
        $nodeVersion = new NodeVersion();
251
        $nodeVersion->setType("public");
252
        $nodeVersion->setNodeTranslation($nodeTranslation);
253
254
        $this->builder->setActiveNodeVersion($nodeVersion);
255
256
        $menu = $this->builder->createActionsMenu();
257
        $this->assertNotNull($menu->getChild('action.recopyfromlanguage'));
258
259
        $this->assertEquals('page-main-actions js-auto-collapse-buttons', $menu->getChildrenAttribute('class'));
260
    }
261
}
262