Passed
Push — developer ( aec883...e4bb52 )
by Radosław
28:28 queued 13:26
created

J_MultiCompany::testCreateRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * MultiCompany test file.
5
 *
6
 * @package   Tests
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Sławomir Rembiesa <[email protected]>
11
 */
12
13
namespace Tests\Base;
14
15
/**
16
 * Class testing creating and setting roles and privileges for MultiCompany records.
17
 */
18
class J_MultiCompany extends \Tests\Base
19
{
20
	/**
21
	 * Temporary User record object
22
	 *
23
	 * @var \Vtiger_Record_Model
24
	 */
25
	protected static $user;
26
27
	/**
28
	 * Temporary MultiCompany record object.
29
	 *
30
	 * @var \Vtiger_Record_Model
31
	 */
32
	protected static $recordMultiCompany;
33
34
	/**
35
	 * Temporary Roles Settings record object.
36
	 *
37
	 * @var \Settings_Roles_Record_Model
38
	 */
39
	protected static $role;
40
41
	/**
42
	 * Creating User module for tests.
43
	 *
44
	 * @return void
45
	 */
46
	public static function createUser(): void
47
	{
48
		$recordModel = \Vtiger_Record_Model::getCleanInstance('Users');
49
		$recordModel->set('user_name', 'TestMultiCompany');
50
		$recordModel->set('email1', "[email protected]");
51
		$recordModel->set('first_name', 'Test');
52
		$recordModel->set('last_name', 'MultiCompany');
53
		$recordModel->set('user_password', 'Demo12345678T');
54
		$recordModel->set('confirm_password', 'Demo12345678T');
55
		$recordModel->set('roleid', self::$role->getId());
56
		$recordModel->set('is_admin', 'on');
57
		$recordModel->save();
58
59
		self::$user = $recordModel;
60
	}
61
62
	/**
63
	 * Creating MultiCompany module record for tests.
64
	 *
65
	 * @return \void
0 ignored issues
show
Bug introduced by
The type void was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
	 */
67
	public static function createMultiCompanyRecord(): void
68
	{
69
		$recordModel = \Vtiger_Record_Model::getCleanInstance('MultiCompany');
70
		$recordModel->set('company_name', 'TestMulti sp. z o.o.');
71
		$recordModel->set('companyid1', '23123214141412');
72
		$recordModel->set('email1', '[email protected]');
73
		$recordModel->set('assigned_user_id', \App\User::getCurrentUserId());
74
		$recordModel->save();
75
76
		self::$recordMultiCompany = $recordModel;
77
	}
78
79
	/**
80
	 * Creating new role for test.
81
	 *
82
	 * @return \void
83
	 */
84
	public static function createRole(): void
85
	{
86
		$recordModel = new \Settings_Roles_Record_Model();
87
		$parentRoleId = 'H1';
88
89
		$parentRole = \Settings_Roles_Record_Model::getInstanceById($parentRoleId);
0 ignored issues
show
Bug introduced by
$parentRoleId of type string is incompatible with the type integer expected by parameter $roleId of Settings_Roles_Record_Model::getInstanceById(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
		$parentRole = \Settings_Roles_Record_Model::getInstanceById(/** @scrutinizer ignore-type */ $parentRoleId);
Loading history...
90
		$recordModel->set('changeowner', 1)
91
			->set('searchunpriv', null)
92
			->set('listrelatedrecord', 0)
93
			->set('previewrelatedrecord',0)
94
			->set('editrelatedrecord', 0)
95
			->set('permissionsrelatedfield', 0)
96
			->set('globalsearchadv', 1)
97
			->set('assignedmultiowner', 1)
98
			->set('clendarallorecords', 3)
99
			->set('company', 1)
100
			->set('auto_assign', 0)
101
			->set('allowassignedrecordsto', 1);
102
103
		$recordModel->set('rolename', 'TestMultiSelect');
104
		$parentRole->addChildRole($recordModel);
105
		$recordModel->save();
106
107
		self::$role = $recordModel;
108
	}
109
110
	/**
111
	 * Setup data to tests.
112
	 *
113
	 * @return void
114
	 */
115
	public static function setUpBeforeClass(): void
116
	{
117
		self::createMultiCompanyRecord();
118
		self::createRole();
119
		self::createUser();
120
	}
121
122
	/**
123
	 * Testing privilege to delete.
124
	 *
125
	 * @return void
126
	 */
127
	public function testPrivilegeToDelete(): void
128
	{
129
		$this->assertTrue(self::$recordMultiCompany->privilegeToDelete());
130
	}
131
132
	/**
133
	 * Testing not privilege to delete.
134
	 *
135
	 * @return void
136
	 */
137
	public function testNotPrivilegeToDelete(): void
138
	{
139
		self::$role->set('company', self::$recordMultiCompany->getId());
140
		self::$role->save();
141
142
		self::$recordMultiCompany->clearPrivilegesCache();
143
144
		$this->assertFalse(self::$recordMultiCompany->privilegeToDelete());
145
	}
146
147
	/**
148
	 * Testing Reloaded by MultiCompany by image loading
149
	 *
150
	 * @return void
151
	 */
152
	public function testReloadByMultiCompany(): void
153
	{
154
		$filePath = ROOT_DIRECTORY . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'MultiImage' . DIRECTORY_SEPARATOR . '0.jpg';
155
		$filePathDestination = ROOT_DIRECTORY . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . '0.jpg';
156
		\copy($filePath, $filePathDestination);
157
		$recordModel = self::$recordMultiCompany;
158
		$fileObj = \App\Fields\File::loadFromPath($filePathDestination);
159
		$hash = $fileObj->generateHash(true, $filePathDestination);
160
		$attach[] = [
0 ignored issues
show
Comprehensibility Best Practice introduced by
$attach was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attach = array(); before regardless.
Loading history...
161
			'name' => '0.jpg',
162
			'size' => $fileObj->getSize(),
163
			'key' => $hash,
164
			'path' => $fileObj->getPath(),
165
			'type' => $fileObj->getMimeType()
166
		];
167
168
		$recordModel->set('logo', \App\Json::encode($attach));
169
		$recordModel->save();
170
171
		$userModel = \App\User::getUserModel(self::$user->getId());
172
		$multiCompanyLogo = $userModel->get('multiCompanyLogo');
173
174
		$fieldModel = $recordModel->getField('logo');
175
		$data = \App\Json::decode(\App\Purifier::decodeHtml($fieldModel->getUITypeModel()->getDisplayValueEncoded($recordModel->get('logo'), $recordModel->getId(), $fieldModel->getFieldInfo())));
0 ignored issues
show
Bug introduced by
The method getDisplayValueEncoded() does not exist on Vtiger_Base_UIType. Did you maybe mean getDisplayValue()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
		$data = \App\Json::decode(\App\Purifier::decodeHtml($fieldModel->getUITypeModel()->/** @scrutinizer ignore-call */ getDisplayValueEncoded($recordModel->get('logo'), $recordModel->getId(), $fieldModel->getFieldInfo())));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
177
		$this->assertSame('0.jpg', $data[0]['name']);
178
		$this->assertSame($fileObj->getSize(), $data[0]['size']);
179
		$this->assertSame($hash, $data[0]['key']);
180
181
		$this->assertSame($multiCompanyLogo['name'], $data[0]['name']);
182
		$this->assertSame($multiCompanyLogo['key'], $data[0]['key']);
183
	}
184
185
	/**
186
	 * Cleaning after tests
187
	 *
188
	 * @return void
189
	 */
190
	public static function tearDownAfterClass(): void
191
	{
192
		self::$recordMultiCompany->delete();
193
		self::$role->delete(\Settings_Roles_Record_Model::getInstanceById('H1'));
0 ignored issues
show
Bug introduced by
'H1' of type string is incompatible with the type integer expected by parameter $roleId of Settings_Roles_Record_Model::getInstanceById(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

193
		self::$role->delete(\Settings_Roles_Record_Model::getInstanceById(/** @scrutinizer ignore-type */ 'H1'));
Loading history...
194
		\Users_Record_Model::deleteUserPermanently(self::$user->getId(), \Users_Record_Model::getCurrentUserModel()->getId());
195
	}
196
}
197