11<?php
2- /**
3- * Pterodactyl - Panel
4- * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5- *
6- * This software is licensed under the terms of the MIT license.
7- * https://opensource.org/licenses/MIT
8- */
92
103namespace Tests \Unit \Services \Servers ;
114
158use Illuminate \Support \Collection ;
169use Pterodactyl \Models \EggVariable ;
1710use Illuminate \Contracts \Validation \Factory ;
18- use Pterodactyl \Exceptions \PterodactylException ;
19- use Pterodactyl \Exceptions \DisplayValidationException ;
11+ use Illuminate \Validation \ValidationException ;
2012use Pterodactyl \Services \Servers \VariableValidatorService ;
2113use Pterodactyl \Contracts \Repository \ServerRepositoryInterface ;
2214use Pterodactyl \Contracts \Repository \EggVariableRepositoryInterface ;
@@ -27,22 +19,17 @@ class VariableValidatorServiceTest extends TestCase
2719 /**
2820 * @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
2921 */
30- protected $ optionVariableRepository ;
22+ private $ optionVariableRepository ;
3123
3224 /**
3325 * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
3426 */
35- protected $ serverRepository ;
27+ private $ serverRepository ;
3628
3729 /**
3830 * @var \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface|\Mockery\Mock
3931 */
40- protected $ serverVariableRepository ;
41-
42- /**
43- * @var \Illuminate\Contracts\Validation\Factory|\Mockery\Mock
44- */
45- protected $ validator ;
32+ private $ serverVariableRepository ;
4633
4734 /**
4835 * Setup tests.
@@ -54,7 +41,6 @@ public function setUp()
5441 $ this ->optionVariableRepository = m::mock (EggVariableRepositoryInterface::class);
5542 $ this ->serverRepository = m::mock (ServerRepositoryInterface::class);
5643 $ this ->serverVariableRepository = m::mock (ServerVariableRepositoryInterface::class);
57- $ this ->validator = m::mock (Factory::class);
5844 }
5945
6046 /**
@@ -77,13 +63,6 @@ public function testValidatorShouldNotProcessVariablesSetAsNotUserEditableWhenAd
7763 $ variables = $ this ->getVariableCollection ();
7864 $ this ->optionVariableRepository ->shouldReceive ('findWhere ' )->with ([['egg_id ' , '= ' , 1 ]])->andReturn ($ variables );
7965
80- $ this ->validator ->shouldReceive ('make ' )->with ([
81- 'variable_value ' => 'Test_SomeValue_0 ' ,
82- ], [
83- 'variable_value ' => $ variables [0 ]->rules ,
84- ])->once ()->andReturnSelf ();
85- $ this ->validator ->shouldReceive ('fails ' )->withNoArgs ()->once ()->andReturn (false );
86-
8766 $ response = $ this ->getService ()->handle (1 , [
8867 $ variables [0 ]->env_variable => 'Test_SomeValue_0 ' ,
8968 $ variables [1 ]->env_variable => 'Test_SomeValue_1 ' ,
@@ -112,15 +91,6 @@ public function testValidatorShouldProcessAllVariablesWhenAdminFlagIsSet()
11291 $ variables = $ this ->getVariableCollection ();
11392 $ this ->optionVariableRepository ->shouldReceive ('findWhere ' )->with ([['egg_id ' , '= ' , 1 ]])->andReturn ($ variables );
11493
115- foreach ($ variables as $ key => $ variable ) {
116- $ this ->validator ->shouldReceive ('make ' )->with ([
117- 'variable_value ' => 'Test_SomeValue_ ' . $ key ,
118- ], [
119- 'variable_value ' => $ variables [$ key ]->rules ,
120- ])->once ()->andReturnSelf ();
121- $ this ->validator ->shouldReceive ('fails ' )->withNoArgs ()->once ()->andReturn (false );
122- }
123-
12494 $ service = $ this ->getService ();
12595 $ service ->setUserLevel (User::USER_LEVEL_ADMIN );
12696 $ response = $ service ->handle (1 , [
@@ -152,28 +122,16 @@ public function testValidatorShouldThrowExceptionWhenAValidationErrorIsEncounter
152122 $ variables = $ this ->getVariableCollection ();
153123 $ this ->optionVariableRepository ->shouldReceive ('findWhere ' )->with ([['egg_id ' , '= ' , 1 ]])->andReturn ($ variables );
154124
155- $ this ->validator ->shouldReceive ('make ' )->with ([
156- 'variable_value ' => null ,
157- ], [
158- 'variable_value ' => $ variables [0 ]->rules ,
159- ])->once ()->andReturnSelf ();
160- $ this ->validator ->shouldReceive ('fails ' )->withNoArgs ()->once ()->andReturn (true );
161-
162- $ this ->validator ->shouldReceive ('errors ' )->withNoArgs ()->once ()->andReturnSelf ();
163- $ this ->validator ->shouldReceive ('toArray ' )->withNoArgs ()->once ()->andReturn ([]);
164-
165125 try {
166126 $ this ->getService ()->handle (1 , [$ variables [0 ]->env_variable => null ]);
167- } catch (PterodactylException $ exception ) {
168- $ this ->assertInstanceOf (DisplayValidationException::class, $ exception );
169-
170- $ decoded = json_decode ($ exception ->getMessage ());
171- $ this ->assertEquals (0 , json_last_error (), 'Assert that response is decodable JSON. ' );
172- $ this ->assertObjectHasAttribute ('notice ' , $ decoded );
173- $ this ->assertEquals (
174- trans ('admin/server.exceptions.bad_variable ' , ['name ' => $ variables [0 ]->name ]),
175- $ decoded ->notice [0 ]
176- );
127+ } catch (ValidationException $ exception ) {
128+ $ messages = $ exception ->validator ->getMessageBag ()->all ();
129+
130+ $ this ->assertNotEmpty ($ messages );
131+ $ this ->assertSame (1 , count ($ messages ));
132+ $ this ->assertSame (trans ('validation.required ' , [
133+ 'attribute ' => trans ('validation.internal.variable_value ' , ['env ' => $ variables [0 ]->name ]),
134+ ]), $ messages [0 ]);
177135 }
178136 }
179137
@@ -205,7 +163,7 @@ private function getService(): VariableValidatorService
205163 $ this ->optionVariableRepository ,
206164 $ this ->serverRepository ,
207165 $ this ->serverVariableRepository ,
208- $ this ->validator
166+ $ this ->app -> make (Factory::class)
209167 );
210168 }
211169}
0 commit comments