FOSSology  4.4.0
Open Source License Compliance by Open Source Software
DefaultPluginTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Plugin;
9 
10 use Exception;
14 use Mockery as M;
15 use Monolog\Logger;
16 use Symfony\Component\DependencyInjection\Container;
17 use Symfony\Component\HttpFoundation\Request;
18 use Symfony\Component\HttpFoundation\Response;
19 use Symfony\Component\HttpFoundation\Session\Session;
20 
22 {
23 
25  private $response;
26 
28  private $request;
29 
30  public function __construct($title, $parameters = array())
31  {
32  parent::__construct($title, $parameters);
33 
34  $this->response = new Response();
35  }
36 
41  protected function handle(Request $request)
42  {
43  $this->request = $request;
44 
45  return $this->response;
46  }
47 
51  public function getTestRequest()
52  {
53  return $this->request;
54  }
55 
59  public function getTestResponse()
60  {
61  return $this->response;
62  }
63 }
64 
65 class DefaultPluginTest extends \PHPUnit\Framework\TestCase
66 {
67  private $name = "<name>";
68 
70  private $logger;
71 
73  private $twigEnvironment;
74 
76  private $session;
77 
79  private $container;
80 
82  private $menu;
83 
85  private $microMenu;
86 
88  private $plugin;
89 
90  protected function setUp() : void
91  {
92  global $SysConf;
93  $SysConf = [];
94  $this->session = M::mock('Symfony\Component\HttpFoundation\Session\SessionInterface');
95 
96  global $container;
97  $container = M::mock('Container');
98 
99  $this->menu = M::mock(Menu::class);
100  $this->twigEnvironment = M::mock('\Twig_Environment');
101  $this->logger = M::mock('Monolog\Logger');
102 
103  $container->shouldReceive('get')->with('ui.component.menu')->andReturn($this->menu);
104  $container->shouldReceive('get')->with('ui.component.micromenu')->andReturn($this->microMenu);
105  $container->shouldReceive('get')->with('twig.environment')->andReturn($this->twigEnvironment);
106  $container->shouldReceive('get')->with('logger')->andReturn($this->logger);
107  $container->shouldReceive('get')->with('session')->andReturn($this->session);
108  $this->container = $container;
109  $GLOBALS['container'] = $container;
110 
111  $this->plugin = new TestPlugin($this->name);
112  }
113 
114  protected function tearDown() : void
115  {
116  M::close();
117  }
118 
119  public function testGetName()
120  {
121  assertThat($this->plugin->getName(), is($this->name));
122  }
123 
124  public function testGetTitle()
125  {
126  assertThat($this->plugin->getTitle(), is(nullValue()));
127 
128  $title = "<title>";
129  $this->plugin = new TestPlugin($this->name, array(TestPlugin::TITLE => $title));
130 
131  assertThat($this->plugin->getTitle(), is($title));
132  }
133 
134  public function testGetPermission()
135  {
136  assertThat($this->plugin->getDBaccess(), is(Auth::PERM_NONE));
137 
138  $this->plugin = new TestPlugin($this->name, array(TestPlugin::PERMISSION => Auth::PERM_WRITE));
139 
140  assertThat($this->plugin->getDBaccess(), is(Auth::PERM_WRITE));
141  }
142 
143  public function testIsRequiresLogin()
144  {
145  $this->assertTrue($this->plugin->isRequiresLogin());
146 
147  $this->plugin = new TestPlugin($this->name, array(TestPlugin::REQUIRES_LOGIN => false));
148 
149  $this->assertFalse($this->plugin->isRequiresLogin());
150  }
151 
152  public function testGetPluginLevel()
153  {
154  assertThat($this->plugin->getPluginLevel(), is(10));
155 
156  $this->plugin = new TestPlugin($this->name, array(TestPlugin::LEVEL => 5));
157 
158  assertThat($this->plugin->getPluginLevel(), is(5));
159  }
160 
161  public function testGetDependencies()
162  {
163  assertThat($this->plugin->getDependency(), is(emptyArray()));
164 
165  $dependencies = array('foo', 'bar');
166  $this->plugin = new TestPlugin($this->name, array(TestPlugin::DEPENDENCIES => $dependencies));
167 
168  assertThat($this->plugin->getDependency(), is($dependencies));
169  }
170 
171  public function testGetInitOrder()
172  {
173  assertThat($this->plugin->getInitOrder(), is(0));
174 
175  $this->plugin = new TestPlugin($this->name, array(TestPlugin::INIT_ORDER => 15));
176 
177  assertThat($this->plugin->getInitOrder(), is(15));
178  }
179 
180  public function testExceptionWhenLoginIsRequired()
181  {
182  $this->expectException(Exception::class);
183  $this->expectExceptionMessage("not allowed without login");
184  $this->plugin->getResponse();
185  }
186 
187  public function testSessionIsWrappedInRequest()
188  {
189  $this->logger->shouldReceive("debug")->once()->with(startsWith("handle request in"));
190 
191  $this->plugin = new TestPlugin($this->name, array(TestPlugin::REQUIRES_LOGIN => false));
192 
193  $this->plugin->getResponse();
194 
195  $request = $this->plugin->getTestRequest();
196 
197  assertThat($request->getSession(), is($this->session));
198  }
199 
200  public function testIsLoggedIn()
201  {
202  global $_SESSION;
203  unset($_SESSION['User']);
204  assertThat($this->plugin->isLoggedIn(), is(equalTo(false)));
205  $_SESSION['User'] = 'Default User';
206  assertThat($this->plugin->isLoggedIn(), is(equalTo(false)));
207  $_SESSION['User'] = 'resU tlaufeD';
208  assertThat($this->plugin->isLoggedIn(), is(equalTo(true)));
209  $this->addToAssertionCount(3);
210  }
211 }
212 
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Code for creating a menu list (2D linked list) from a set of plugins.
Definition: common-menu.php:20
int response
Is a response expected from the scheduler.
Definition: fo_cli.c:36