Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

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