Skip to content
This repository was archived by the owner on May 1, 2019. It is now read-only.

Commit ec27d0f

Browse files
committed
Dropped direct rendering of user's module list from within the view
- Provide separate controller action for rendering list (renderModuleListAction) - Inject module list (via child model) rather than relying on view helper to load it - Update module-view helper view script to correct route add/delete references
1 parent 6cf45d0 commit ec27d0f

File tree

7 files changed

+139
-80
lines changed

7 files changed

+139
-80
lines changed

module/User/config/module.config.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,62 @@
3232
],
3333
],
3434
'user' => [
35-
'type' => 'Segment',
35+
'type' => 'Literal',
3636
'options' => [
3737
'route' => '/user',
3838
'defaults' => [
3939
'controller' => 'User\Controller\Index',
4040
'action' => 'index',
4141
],
4242
],
43+
'may_terminate' => true,
44+
'child_routes' => [
45+
'module' => [
46+
'type' => 'Literal',
47+
'options' => [
48+
'route' => '/module',
49+
],
50+
'may_terminate' => false,
51+
'child_routes' => [
52+
'list' => [
53+
'type' => 'Literal',
54+
'options' => [
55+
'route' => '/render-list',
56+
'defaults' => [
57+
'action' => 'render-module-list',
58+
],
59+
],
60+
],
61+
'add' => [
62+
'type' => 'Literal',
63+
'options' => [
64+
'route' => '/add',
65+
'defaults' => [
66+
'action' => 'add',
67+
],
68+
],
69+
],
70+
'remove' => [
71+
'type' => 'Segment',
72+
'options' => [
73+
'route' => '/:module_id/remove',
74+
'constraints' => [
75+
'module_id' => '[0-9]+',
76+
],
77+
'defaults' => [
78+
'action' => 'remove',
79+
],
80+
],
81+
],
82+
],
83+
],
84+
],
4385
],
4486
],
4587
],
4688
'controllers' => [
47-
'invokables' => [
48-
'User\Controller\Index' => 'User\Controller\IndexController',
89+
'factories' => [
90+
'User\Controller\Index' => 'User\Controller\IndexControllerFactory',
4991
],
5092
],
5193
'view_helpers' => [

module/User/src/User/Controller/IndexController.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,42 @@
44

55
use Zend\Mvc\Controller\AbstractActionController;
66
use Zend\View\Model\ViewModel;
7+
use ZfModule\Mapper\Module as ModuleMapper;
78

89
class IndexController extends AbstractActionController
910
{
11+
/**
12+
* @var ModuleMapper
13+
*/
14+
protected $moduleMapper;
15+
16+
/**
17+
* @param ModuleMapper $mapper
18+
*/
19+
public function __construct(ModuleMapper $mapper)
20+
{
21+
$this->moduleMapper = $mapper;
22+
}
23+
1024
public function indexAction()
1125
{
26+
$registeredModules = $this->renderModuleListAction();
27+
$registeredModules->setTerminal(false);
28+
29+
$vm = new ViewModel();
30+
$vm->addChild($registeredModules, 'registered_modules');
31+
32+
return $vm;
33+
}
34+
35+
public function renderModuleListAction()
36+
{
37+
$modules = $this->moduleMapper->findByOwner($this->zfcUserAuthentication()->getIdentity()->getId());
38+
1239
$vm = new ViewModel();
40+
$vm->setTemplate('user/index/render-module-list');
41+
$vm->setVariable('modules', $modules);
42+
$vm->setTerminal(true);
1343

1444
return $vm;
1545
}

module/User/src/User/Controller/IndexControllerFactory.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace User\Controller;
44

5+
use Zend\ServiceManager\FactoryInterface;
6+
use Zend\ServiceManager\ServiceLocatorInterface;
7+
use ZfModule\Mapper\Module as ModuleMapper;
8+
59
class IndexControllerFactory implements FactoryInterface
610
{
711
/**
@@ -10,6 +14,14 @@ class IndexControllerFactory implements FactoryInterface
1014
*/
1115
public function createService(ServiceLocatorInterface $controllerManager)
1216
{
13-
return new IndexController();
17+
/* @var ServiceLocatorInterface $controllerManager */
18+
$serviceManager = $controllerManager->getServiceLocator();
19+
20+
/* @var ModuleMapper $moduleMapper */
21+
$moduleMapper = $serviceManager->get('zfmodule_mapper_module');
22+
23+
return new IndexController(
24+
$moduleMapper
25+
);
1426
}
1527
}

module/User/test/UserTest/Integration/Controller/IndexControllerTest.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
use ApplicationTest\Integration\Util\Bootstrap;
66
use User\Entity\User as UserEntity;
7+
use Zend\Authentication\AuthenticationService;
8+
use Zend\Db\ResultSet\HydratingResultSet;
79
use Zend\Http\Response as HttpResponse;
10+
use Zend\Stdlib\Hydrator\ClassMethods;
811
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
12+
use ZfModule\Entity\Module as ModuleEntity;
13+
use ZfModule\Mapper\Module as ModuleMapper;
14+
use ZfModule\View\Helper\TotalModules;
915

1016
class IndexControllerTest extends AbstractHttpControllerTestCase
1117
{
@@ -17,19 +23,14 @@ protected function setUp()
1723
$this->setApplicationConfig(Bootstrap::getConfig());
1824
$this->getApplicationServiceLocator()->setAllowOverride(true);
1925

20-
$mockTotalModules = $this->getMockBuilder('ZfModule\View\Helper\TotalModules')
26+
$mockTotalModules = $this->getMockBuilder(TotalModules::class)
2127
->disableOriginalConstructor()
2228
->getMock();
2329

24-
$mockListModule = $this->getMockBuilder('ZfModule\View\Helper\ListModule')
25-
->disableOriginalConstructor()
26-
->getMock();
27-
2830
$this->viewHelperManager = $this->getApplicationServiceLocator()->get('ViewHelperManager');
2931
$this->viewHelperManager->setService('totalModules', $mockTotalModules);
30-
$this->viewHelperManager->setService('listModule', $mockListModule);
3132

32-
$mockAuthService = $this->getMockBuilder('Zend\Authentication\AuthenticationService')
33+
$mockAuthService = $this->getMockBuilder(AuthenticationService::class)
3334
->disableOriginalConstructor()
3435
->getMock();
3536
$mockAuthService->expects($this->any())
@@ -38,23 +39,40 @@ protected function setUp()
3839
$mockAuthService->expects($this->any())
3940
->method('getIdentity')
4041
->will($this->returnValue(new UserEntity()));
41-
4242
$this->getApplicationServiceLocator()->setService('zfcuser_auth_service', $mockAuthService);
4343
$this->viewHelperManager->get('zfcUserIdentity')->setAuthService($mockAuthService);
44+
45+
$mockMapper = $this->getMockBuilder(ModuleMapper::class)
46+
->disableOriginalConstructor()
47+
->getMock();
48+
$this->getApplicationServiceLocator()->setService('zfmodule_mapper_module', $mockMapper);
4449
}
4550

46-
public function testIndexActionCanBeAccessed()
51+
/**
52+
* @group integration
53+
*/
54+
public function testIndexActionRendersUserModuleList()
4755
{
48-
$mockListModule = $this->viewHelperManager->get('listModule');
49-
$mockListModule->expects($this->once())
50-
->method('__invoke')
51-
->will($this->returnValue([]));
56+
$mockResultSet = new HydratingResultSet(new ClassMethods(false), new ModuleEntity());
57+
$mockResultSet->initialize([[
58+
'id' => 123,
59+
'name' => 'FooModule',
60+
'description' => 'some random module',
61+
'url' => 'https://github.com/zendframework/modules.zendframework.com',
62+
]]);
63+
64+
$mockMapper = $this->getApplicationServiceLocator()->get('zfmodule_mapper_module');
65+
$mockMapper->expects($this->once())
66+
->method('findByOwner')
67+
->willReturn($mockResultSet);
5268

5369
$this->dispatch('/user');
5470

5571
$this->assertControllerName('User\Controller\Index');
5672
$this->assertActionName('index');
5773
$this->assertTemplateName('user/index/index');
5874
$this->assertResponseStatusCode(HttpResponse::STATUS_CODE_200);
75+
$this->assertContains('FooModule', $this->getResponse()->getContent());
76+
$this->assertContains('/user/module/123/remove', $this->getResponse()->getContent());
5977
}
6078
}

module/User/view/user/index/index.phtml

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,7 @@
1717

1818
<div class="tab-content">
1919
<div class="tab-pane active" id="modules">
20-
<?php
21-
$modules = $this->listModule(['user' => true]);
22-
if (empty($modules)) {
23-
?>
24-
<div class="alert alert-info alert-block"><?php echo $this->translate('You have not added any modules'); ?></div>
25-
<?php
26-
}
27-
foreach ($modules as $module) {
28-
echo $this->moduleView([
29-
'owner' => $module->getOwner(),
30-
'name' => $module->getName(),
31-
'created_at' => $module->getCreatedAt(),
32-
'url' => $module->getUrl(),
33-
'photo_url' => $module->getPhotoUrl(),
34-
'description' => $module->getDescription(),
35-
], 'remove');
36-
}
37-
?>
20+
<?php echo $this->registered_modules; ?>
3821
</div>
3922
<div class="tab-pane" id="addrepo">
4023

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,17 @@
1-
2-
<div style="margin-bottom: 20px;">
3-
<img style="float:left; height: 80px; padding-right: 10px;" src="<?php echo $this->zfcUserIdentity()->getPhotoUrl() ?>" alt="<?php echo $this->zfcUserDisplayName() ?>"/>
4-
<h3 style="margin-bottom: 0px;">Hello, <?php echo $this->zfcUserDisplayName() ?>!</h3>
5-
<p>We should fill this space with some useful information...</p>
6-
<div class="clearfix"></div>
7-
</div>
8-
9-
<?php foreach ($this->flashMessenger()->getMessages() as $message): ?>
10-
<h3 class="zf-green"><?php echo $message ?></h3>
11-
<?php endforeach; ?>
12-
13-
<ul class="nav nav-tabs">
14-
<li class="active"><a href="#modules" data-toggle="tab"><?php echo $this->translate('Your Registered Modules'); ?></a></li>
15-
<li><a href="#addrepo" data-toggle="tab"><?php echo $this->translate('Add a New Module'); ?></a></li>
16-
</ul>
17-
18-
<div class="tab-content">
19-
<div class="tab-pane active" id="modules">
20-
<?php
21-
$modules = $this->listModule(['user' => true]);
22-
if (empty($modules)) {
23-
?>
24-
<div class="alert alert-info alert-block"><?php echo $this->translate('You have not added any modules'); ?></div>
25-
<?php
26-
}
27-
foreach ($modules as $module) {
28-
echo $this->moduleView([
29-
'owner' => $module->getOwner(),
30-
'name' => $module->getName(),
31-
'created_at' => $module->getCreatedAt(),
32-
'url' => $module->getUrl(),
33-
'photo_url' => $module->getPhotoUrl(),
34-
'description' => $module->getDescription(),
35-
], 'remove');
36-
}
37-
?>
38-
</div>
39-
<div class="tab-pane" id="addrepo">
40-
41-
</div>
42-
</div>
43-
1+
<?php
2+
if ($this->modules->count() === 0) {
3+
?>
4+
<div class="alert alert-info alert-block"><?php echo $this->translate('You have not added any modules'); ?></div>
5+
<?php
6+
}
7+
foreach ($this->modules as $module) {
8+
echo $this->moduleView([
9+
'id' => $module->getId(),
10+
'owner' => $module->getOwner(),
11+
'name' => $module->getName(),
12+
'created_at' => $module->getCreatedAt(),
13+
'url' => $module->getUrl(),
14+
'photo_url' => $module->getPhotoUrl(),
15+
'description' => $module->getDescription(),
16+
], 'remove');
17+
}

module/ZfModule/view/zf-module/helper/module-view.phtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
<div class="span1">
2727
<br />
2828
<?php if ($button == 'submit'): ?>
29-
<form action="<?php echo $this->url('zf-module/add') ?>" method="post">
29+
<form action="<?php echo $this->url('user/module/add') ?>" method="post">
3030
<input type="hidden" name="owner" value="<?php echo $this->escapeHtmlAttr($module['owner']) ?>" />
3131
<input type="hidden" name="repo" value="<?php echo $this->escapeHtmlAttr($module['name']) ?>" />
3232
<input type="submit" class="btn btn-success"/>
3333
</form>
3434
<?php else: ?>
35-
<form action="<?php echo $this->url('zf-module/remove') ?>" method="post">
35+
<form action="<?php echo $this->url('user/module/remove', ['module_id' => $module['id']]) ?>" method="post">
3636
<input type="hidden" name="owner" value="<?php echo $this->escapeHtmlAttr($module['owner']) ?>" />
3737
<input type="hidden" name="repo" value="<?php echo $this->escapeHtmlAttr($module['name']) ?>" />
3838
<input type="submit" value="Remove" class="btn btn-danger"/>

0 commit comments

Comments
 (0)