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  $this->session = M::mock('Symfony\Component\HttpFoundation\Session\SessionInterface');
93 
94  global $container;
95  $container = M::mock('Container');
96 
97  $this->menu = M::mock(Menu::class);
98  $this->twigEnvironment = M::mock('\Twig_Environment');
99  $this->logger = M::mock('Monolog\Logger');
100 
101  $container->shouldReceive('get')->with('ui.component.menu')->andReturn($this->menu);
102  $container->shouldReceive('get')->with('ui.component.micromenu')->andReturn($this->microMenu);
103  $container->shouldReceive('get')->with('twig.environment')->andReturn($this->twigEnvironment);
104  $container->shouldReceive('get')->with('logger')->andReturn($this->logger);
105  $container->shouldReceive('get')->with('session')->andReturn($this->session);
106  $this->container = $container;
107  $GLOBALS['container'] = $container;
108 
109  $this->plugin = new TestPlugin($this->name);
110  }
111 
112  protected function tearDown() : void
113  {
114  M::close();
115  }
116 
117  public function testGetName()
118  {
119  assertThat($this->plugin->getName(), is($this->name));
120  }
121 
122  public function testGetTitle()
123  {
124  assertThat($this->plugin->getTitle(), is(nullValue()));
125 
126  $title = "<title>";
127  $this->plugin = new TestPlugin($this->name, array(TestPlugin::TITLE => $title));
128 
129  assertThat($this->plugin->getTitle(), is($title));
130  }
131 
132  public function testGetPermission()
133  {
134  assertThat($this->plugin->getDBaccess(), is(Auth::PERM_NONE));
135 
136  $this->plugin = new TestPlugin($this->name, array(TestPlugin::PERMISSION => Auth::PERM_WRITE));
137 
138  assertThat($this->plugin->getDBaccess(), is(Auth::PERM_WRITE));
139  }
140 
141  public function testIsRequiresLogin()
142  {
143  $this->assertTrue($this->plugin->isRequiresLogin());
144 
145  $this->plugin = new TestPlugin($this->name, array(TestPlugin::REQUIRES_LOGIN => false));
146 
147  $this->assertFalse($this->plugin->isRequiresLogin());
148  }
149 
150  public function testGetPluginLevel()
151  {
152  assertThat($this->plugin->getPluginLevel(), is(10));
153 
154  $this->plugin = new TestPlugin($this->name, array(TestPlugin::LEVEL => 5));
155 
156  assertThat($this->plugin->getPluginLevel(), is(5));
157  }
158 
159  public function testGetDependencies()
160  {
161  assertThat($this->plugin->getDependency(), is(emptyArray()));
162 
163  $dependencies = array('foo', 'bar');
164  $this->plugin = new TestPlugin($this->name, array(TestPlugin::DEPENDENCIES => $dependencies));
165 
166  assertThat($this->plugin->getDependency(), is($dependencies));
167  }
168 
169  public function testGetInitOrder()
170  {
171  assertThat($this->plugin->getInitOrder(), is(0));
172 
173  $this->plugin = new TestPlugin($this->name, array(TestPlugin::INIT_ORDER => 15));
174 
175  assertThat($this->plugin->getInitOrder(), is(15));
176  }
177 
178  public function testExceptionWhenLoginIsRequired()
179  {
180  $this->expectException(Exception::class);
181  $this->expectExceptionMessage("not allowed without login");
182  $this->plugin->getResponse();
183  }
184 
185  public function testSessionIsWrappedInRequest()
186  {
187  $this->logger->shouldReceive("debug")->once()->with(startsWith("handle request in"));
188 
189  $this->plugin = new TestPlugin($this->name, array(TestPlugin::REQUIRES_LOGIN => false));
190 
191  $this->plugin->getResponse();
192 
193  $request = $this->plugin->getTestRequest();
194 
195  assertThat($request->getSession(), is($this->session));
196  }
197 
198  public function testIsLoggedIn()
199  {
200  global $_SESSION;
201  unset($_SESSION['User']);
202  assertThat($this->plugin->isLoggedIn(), is(equalTo(false)));
203  $_SESSION['User'] = 'Default User';
204  assertThat($this->plugin->isLoggedIn(), is(equalTo(false)));
205  $_SESSION['User'] = 'resU tlaufeD';
206  assertThat($this->plugin->isLoggedIn(), is(equalTo(true)));
207  $this->addToAssertionCount(3);
208  }
209 }
210 
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