FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UserTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
14 
20 use Mockery as M;
21 
22 require_once dirname(dirname(dirname(dirname(__DIR__)))) .
23  '/lib/php/Plugin/FO_Plugin.php';
24 
29 class UserTest extends \PHPUnit\Framework\TestCase
30 {
40  private $dbHelper;
45  private $userDao;
46 
51  private $restHelper;
56  protected function setUp() : void
57  {
58  global $container;
59  $container = M::mock('ContainerBuilder');
60  $this->restHelper = M::mock(RestHelper::class);
61  $this->userDao = M::mock(UserDao::class);
62 
63  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
64  $this->restHelper->shouldReceive('getUserDao')
65  ->andReturn($this->userDao);
66 
67  $container->shouldReceive('get')->withArgs(array(
68  'helper.restHelper'))->andReturn($this->restHelper);
69  }
70 
75  protected function tearDown() : void
76  {
77  $this->addToAssertionCount(
78  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
79  M::close();
80  }
81 
95  public function testDataFormatV1()
96  {
97  $this->testDataFormat(ApiVersion::V1);
98  }
99 
107  public function testDataFormatV2()
108  {
109  $this->testDataFormat(ApiVersion::V2);
110  }
111 
117  private function testDataFormat($version)
118  {
119  if($version==ApiVersion::V1){
120  $expectedCurrentUser = [
121  "id" => 2,
122  "name" => 'fossy',
123  "description" => 'super user',
124  "email" => 'fossy@localhost',
125  "accessLevel" => 'admin',
126  "rootFolderId" => 2,
127  "defaultGroup" => 0,
128  "emailNotification" => true,
129  "agents" => [
130  "bucket" => true,
131  "copyright_email_author" => true,
132  "ecc" => false,
133  "keyword" => false,
134  "mimetype" => false,
135  "monk" => false,
136  "nomos" => true,
137  "ojo" => true,
138  "package" => false,
139  "reso" => false,
140  "compatibility" => false
141  ]
142  ];
143  } else{
144  $expectedCurrentUser = [
145  "id" => 2,
146  "name" => 'fossy',
147  "description" => 'super user',
148  "email" => 'fossy@localhost',
149  "accessLevel" => 'admin',
150  "rootFolderId" => 2,
151  "defaultGroup" => "fossy",
152  "emailNotification" => true,
153  "agents" => [
154  "bucket" => true,
155  "copyrightEmailAuthor" => true,
156  "ecc" => false,
157  "keyword" => false,
158  "mimetype" => false,
159  "monk" => false,
160  "nomos" => true,
161  "ojo" => true,
162  "package" => false,
163  "reso" => false,
164  "compatibility" => false
165  ]
166  ];
167  }
168  $expectedNonAdminUser = [
169  "id" => 8,
170  "name" => 'userii',
171  "description" => 'very useri',
172  "defaultGroup" => 0,
173  ];
174 
175  $actualCurrentUserV1 = new User(2, 'fossy', 'super user', 'fossy@localhost',
176  PLUGIN_DB_ADMIN, 2, true, "bucket,copyright,nomos,ojo", 0);
177 
178  $this->userDao->shouldReceive('getGroupNameById')->withArgs([0])->andReturn("fossy");
179 
180  $actualCurrentUserV2 = new User(2, 'fossy', 'super user', 'fossy@localhost',
181  PLUGIN_DB_ADMIN, 2, true, "bucket,copyright,nomos,ojo", "fossy");
182  $actualNonAdminUser = new User(8, 'userii', 'very useri', null, null, null,
183  null, null, 0);
184  if ($version == ApiVersion::V2) {
185  $this->assertEquals($expectedCurrentUser, $actualCurrentUserV2->getArray($version));
186  } else {
187  $this->assertEquals($expectedCurrentUser, $actualCurrentUserV1->getArray($version));
188  }
189  $this->assertEquals($expectedNonAdminUser, $actualNonAdminUser->getArray());
190  }
191 }
Provides helper methods to access database for REST api.
Definition: DbHelper.php:38
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32
Model to hold user information.
Definition: User.php:21
tearDown()
Remove test objects.
Definition: UserTest.php:75
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39