FOSSology  4.4.0
Open Source License Compliance by Open Source Software
MaintenanceControllerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Samuel Dushimimana <dushsam100@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
14 
24 use Mockery as M;
25 use Slim\Psr7\Factory\StreamFactory;
26 use Slim\Psr7\Uri;
27 use Slim\Psr7\Headers;
28 use Slim\Psr7\Request;
29 
30 require_once dirname(__DIR__, 4) . '/lib/php/Plugin/FO_Plugin.php';
31 
36 class MaintenanceControllerTest extends \PHPUnit\Framework\TestCase
37 {
38 
39 
44  const YAML_LOC = __DIR__ . '/../../../ui/api/documentation/openapi.yaml';
45 
51 
57 
62  private $dbHelper;
63 
68  private $restHelper;
69 
74  private $auth;
75 
81 
91  protected function setUp() : void
92  {
93  global $container;
94  $container = M::mock('ContainerBuilder');
95  $this->dbHelper = M::mock(DbHelper::class);
96  $this->restHelper = M::mock(RestHelper::class);
97  $this->userDao = M::mock(UserDao::class);
98  $this->auth = M::mock(Auth::class);
99 
100  $this->maintagentPlugin = M::mock('maintagent');
101 
102  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
103  $this->restHelper->shouldReceive('getUserDao')
104  ->andReturn($this->userDao);
105 
106  $this->restHelper->shouldReceive('getPlugin')
107  ->withArgs(array('maintagent'))
108  ->andReturn($this->maintagentPlugin);
109 
110  $this->auth->shouldReceive('isAdmin')->andReturn(true);
111 
112  $container->shouldReceive('get')->withArgs(array(
113  'helper.restHelper'))->andReturn($this->restHelper);
114  $this->maintenanceController = new MaintenanceController($container);
115  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
116  $this->dbManager = M::mock(DbManager::class);
117  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
118  $this->streamFactory = new StreamFactory();
119 
120  }
121 
128  private function getResponseJson($response)
129  {
130  $response->getBody()->seek(0);
131  return json_decode($response->getBody()->getContents(), true);
132  }
133 
139  public function testCreateMaintenance()
140  {
141  $_SESSION['UserLevel'] = 10;
142 
143  $rq = [
144  "options" => ["A","F","g","l","o"],
145  "logsDate"=>"2021-08-19",
146  "goldDate"=>"2022-07-16"
147  ];
148 
149  $OPTIONS =[
150  "A"=>"Run all maintenance operations.",
151  "F"=>"Validate folder contents.",
152  "g"=>"Remove orphaned gold files.",
153  "o"=>"Remove older gold files from repository.",
154  "l"=>"Remove older log files from repository."
155  ];
156 
157  $alteredOptions = array();
158  foreach ($rq['options'] as $key) {
159  $alteredOptions[$key] = $key;
160  }
161  $body = $rq;
162  $body['options'] = $alteredOptions;
163 
164  $this->maintagentPlugin->shouldReceive('getOptions')->andReturn($OPTIONS);
165 
166  $mess = _("The maintenance job has been queued");
167 
168  $this->maintagentPlugin->shouldReceive('handle')->withArgs([$body])->andReturn($mess);
169 
170  $info = new Info(201, $mess, InfoType::INFO);
171 
172 
173  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
174 
175  $reqBody = $this->streamFactory->createStream(json_encode(
176  $rq
177  ));
178 
179  $requestHeaders = new Headers();
180  $requestHeaders->setHeader('Content-Type', 'application/json');
181  $request = new Request("POST", new Uri("HTTP", "localhost"),
182  $requestHeaders, [], [], $reqBody);
183 
184 
185  $actualResponse = $this->maintenanceController->createMaintenance($request, new ResponseHelper(), null);
186 
187  $this->assertEquals($expectedResponse->getStatusCode(),
188  $actualResponse->getStatusCode());
189  $this->assertEquals($this->getResponseJson($expectedResponse),
190  $this->getResponseJson($actualResponse));
191  }
192 
193 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Provides helper methods to access database for REST api.
Definition: DbHelper.php:38
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16