FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
DefaultPluginTest.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Plugin;
20 
21 use Exception;
25 use Monolog\Logger;
30 use Mockery as M;
31 
33 {
34 
36  private $response;
37 
39  private $request;
40 
41  public function __construct($title, $parameters = array())
42  {
43  parent::__construct($title, $parameters);
44 
45  $this->response = Response::create();
46  }
47 
52  protected function handle(Request $request)
53  {
54  $this->request = $request;
55 
56  return $this->response;
57  }
58 
62  public function getTestRequest()
63  {
64  return $this->request;
65  }
66 
70  public function getTestResponse()
71  {
72  return $this->response;
73  }
74 }
75 
76 class DefaultPluginTest extends \PHPUnit\Framework\TestCase
77 {
78  private $name = "<name>";
79 
81  private $logger;
82 
84  private $twigEnvironment;
85 
87  private $session;
88 
90  private $container;
91 
93  private $menu;
94 
96  private $microMenu;
97 
99  private $plugin;
100 
101  protected function setUp()
102  {
103  $this->session = M::mock('Symfony\Component\HttpFoundation\Session\SessionInterface');
104 
105  global $container;
106  $container = M::mock('Container');
107 
108  $this->menu = M::mock(Menu::class);
109  $this->twigEnvironment = M::mock('\Twig_Environment');
110  $this->logger = M::mock('Monolog\Logger');
111 
112  $container->shouldReceive('get')->with('ui.component.menu')->andReturn($this->menu);
113  $container->shouldReceive('get')->with('ui.component.micromenu')->andReturn($this->microMenu);
114  $container->shouldReceive('get')->with('twig.environment')->andReturn($this->twigEnvironment);
115  $container->shouldReceive('get')->with('logger')->andReturn($this->logger);
116  $container->shouldReceive('get')->with('session')->andReturn($this->session);
117  $this->container = $container;
118  $GLOBALS['container'] = $container;
119 
120  $this->plugin = new TestPlugin($this->name);
121  }
122 
123  protected function tearDown()
124  {
125  M::close();
126  }
127 
128  public function testGetName()
129  {
130  assertThat($this->plugin->getName(), is($this->name));
131  }
132 
133  public function testGetTitle()
134  {
135  assertThat($this->plugin->getTitle(), is(nullValue()));
136 
137  $title = "<title>";
138  $this->plugin = new TestPlugin($this->name, array(TestPlugin::TITLE => $title));
139 
140  assertThat($this->plugin->getTitle(), is($title));
141  }
142 
143  public function testGetPermission()
144  {
145  assertThat($this->plugin->getDBaccess(), is(Auth::PERM_NONE));
146 
147  $this->plugin = new TestPlugin($this->name, array(TestPlugin::PERMISSION => Auth::PERM_WRITE));
148 
149  assertThat($this->plugin->getDBaccess(), is(Auth::PERM_WRITE));
150  }
151 
152  public function testIsRequiresLogin()
153  {
154  $this->assertTrue($this->plugin->isRequiresLogin());
155 
156  $this->plugin = new TestPlugin($this->name, array(TestPlugin::REQUIRES_LOGIN => false));
157 
158  $this->assertFalse($this->plugin->isRequiresLogin());
159  }
160 
161  public function testGetPluginLevel()
162  {
163  assertThat($this->plugin->getPluginLevel(), is(10));
164 
165  $this->plugin = new TestPlugin($this->name, array(TestPlugin::LEVEL => 5));
166 
167  assertThat($this->plugin->getPluginLevel(), is(5));
168  }
169 
170  public function testGetDependencies()
171  {
172  assertThat($this->plugin->getDependency(), is(emptyArray()));
173 
174  $dependencies = array('foo', 'bar');
175  $this->plugin = new TestPlugin($this->name, array(TestPlugin::DEPENDENCIES => $dependencies));
176 
177  assertThat($this->plugin->getDependency(), is($dependencies));
178  }
179 
180  public function testGetInitOrder()
181  {
182  assertThat($this->plugin->getInitOrder(), is(0));
183 
184  $this->plugin = new TestPlugin($this->name, array(TestPlugin::INIT_ORDER => 15));
185 
186  assertThat($this->plugin->getInitOrder(), is(15));
187  }
188 
194  {
195  $this->plugin->getResponse();
196  }
197 
198  public function testSessionIsWrappedInRequest()
199  {
200  $this->logger->shouldReceive("debug")->once()->with(startsWith("handle request in"));
201 
202  $this->plugin = new TestPlugin($this->name, array(TestPlugin::REQUIRES_LOGIN => false));
203 
204  $this->plugin->getResponse();
205 
206  $request = $this->plugin->getTestRequest();
207 
208  assertThat($request->getSession(), is($this->session));
209  }
210 
211  public function testIsLoggedIn()
212  {
213  global $_SESSION;
214  unset($_SESSION['User']);
215  assertThat($this->plugin->isLoggedIn(), is(equalTo(false)));
216  $_SESSION['User'] = 'Default User';
217  assertThat($this->plugin->isLoggedIn(), is(equalTo(false)));
218  $_SESSION['User'] = 'resU tlaufeD';
219  assertThat($this->plugin->isLoggedIn(), is(equalTo(true)));
220  $this->addToAssertionCount(3);
221  }
222 }
223 
int response
Is a response expected from the scheduler.
Definition: fo_cli.c:47
Code for creating a menu list (2D linked list) from a set of plugins.
Definition: common-menu.php:30