Files
FOSSBilling/tests/modules/System/Api/GuestTest.php
T
Christoph SchlaepferandBelle Aerni 7a38450458 Make system version & company info visibility through the guest API configurable (#1504)
* Add feature to hide public FOSSBilling version and Company information

* Cleaned up

* Added Test cases for Version & Company information

* Fixed Authorization Mock

* Fixed Tests so isAdminLoggedIn is false when testing Public Access

* Cleaned up

* Implemented Suggested code and fixed tests.

* Removed manual Version for API.js string and replaced with twig filter "script_tag"

* Added Twig filter for Version and replaced guest.system_version

* Added Message to Admin Settings

* Now let's do all of that, but better

- A filter is the wrong way to get the system version to the front end
- The wording in the admin panel for these settings didn't correctly describe what they did and they had poor grammar
- The version check should be returning a string, not an array & the code can be significantly cleaned up.
- Inverting the behavior of these toggles means that existing installations will continue to have the same behavior as before without us forcibly changing it. Otherwise, we need to introduce a DB patch to add the new values with them being on to match what was the old behavior.
- For new installations, we can pretty safely turn these options off to give a bit more privacy / security and allow them to be turned back on if wanted.
- New installations can keep the current behavior. We will highlight that this feature is added & if it's something the administrator wants, they can adjust the settings for themselves

* Cursed spelling *angry hand movements*

* That should have never been an array to being with

---------

Co-authored-by: Belle Aerni <belleaerni@gmail.com>
2023-08-02 19:40:43 +02:00

323 lines
10 KiB
PHP

<?php
namespace Box\Mod\System\Api;
class GuestTest extends \BBTestCase {
/**
* @var \Box\Mod\System\Api\Guest
*/
protected $api = null;
public function setup(): void
{
$this->api= new \Box\Mod\System\Api\Guest();
}
public function testgetDi()
{
$di = new \Pimple\Container();
$this->api->setDi($di);
$getDi = $this->api->getDi();
$this->assertEquals($di, $getDi);
}
public function testVersionAdmin()
{
$authorizationMock = $this->getMockBuilder('\Box_Authorization')->disableOriginalConstructor()->getMock();
$authorizationMock->expects($this->atLeastOnce())
->method("isAdminLoggedIn")
->willReturn(true);
$di = new \Pimple\Container();
$di['auth'] = $authorizationMock;
$serviceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getVersion')
->willReturn(\FOSSBilling\Version::VERSION);
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->version();
$this->assertIsString($result);
$this->assertNotEmpty($result);
}
public function testVersionShowPublicOn()
{
$authorizationMock = $this->getMockBuilder('\Box_Authorization')->disableOriginalConstructor()->getMock();
$authorizationMock->expects($this->atLeastOnce())
->method("isAdminLoggedIn")
->willReturn(false);
$di = new \Pimple\Container();
$di['auth'] = $authorizationMock;
$serviceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getVersion')
->willReturn(\FOSSBilling\Version::VERSION);
$serviceMock->expects($this->atLeastOnce())
->method('getParamValue')
->with('hide_version_public')
->willReturn(0);
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->version();
$this->assertIsString($result);
$this->assertNotEmpty($result);
}
public function testVersionShowPublicOff()
{
$authorizationMock = $this->getMockBuilder('\Box_Authorization')->disableOriginalConstructor()->getMock();
$authorizationMock->expects($this->atLeastOnce())
->method("isAdminLoggedIn")
->willReturn(false);
$di = new \Pimple\Container();
$di['auth'] = $authorizationMock;
$serviceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getParamValue')
->with('hide_version_public')
->willReturn(1);
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->version();
$this->assertIsString($result);
$this->assertEmpty($result);
}
public function testCompanyShowPublicOn()
{
$companyData = array('companyName' => 'TestCo');
$authMock = $this->getMockBuilder('\Box_Authorization')->disableOriginalConstructor()->getMock();
$authMock->method('isAdminLoggedIn')->willReturn(false);
$authMock->method('isClientLoggedIn')->willReturn(false);
$serviceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getCompany')
->willReturn($companyData);
$serviceMock->expects($this->atLeastOnce())
->method('getParamValue')
->with('hide_company_public')
->willReturn(0);
$di = new \Pimple\Container();
$di['auth'] = $authMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->company();
$this->assertIsArray($result);
$this->assertNotEmpty($result);
}
public function testCompanyShowPublicOff()
{
$companyData = array(
'companyName' => 'TestCo',
'vat_number' => 'Test VAT',
'email' => 'test@email.com',
'tel' => '123456789',
'account_number' => '987654321',
'number' => '123456',
'address_1' => 'Test Address 1',
'address_2' => 'Test Address 2',
'address_3' => 'Test Address 3',
);
$authMock = $this->getMockBuilder('\Box_Authorization')->disableOriginalConstructor()->getMock();
$authMock->method('isAdminLoggedIn')->willReturn(false);
$authMock->method('isClientLoggedIn')->willReturn(false);
$serviceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getCompany')
->willReturn($companyData);
$serviceMock->expects($this->atLeastOnce())
->method('getParamValue')
->with('hide_company_public')
->willReturn(1);
$di = new \Pimple\Container();
$di['auth'] = $authMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->company();
$this->assertIsArray($result);
$this->assertArrayNotHasKey('vat_number', $result);
$this->assertArrayNotHasKey('email', $result);
$this->assertArrayNotHasKey('tel', $result);
$this->assertArrayNotHasKey('account_number', $result);
$this->assertArrayNotHasKey('number', $result);
$this->assertArrayNotHasKey('address_1', $result);
$this->assertArrayNotHasKey('address_2', $result);
$this->assertArrayNotHasKey('address_3', $result);
}
public function testphone_codes()
{
$data = array(
);
$servuceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$servuceMock->expects($this->atLeastOnce())
->method('getPhoneCodes')
->will($this->returnValue(array()));
$this->api->setService($servuceMock);
$result = $this->api->phone_codes($data);
$this->assertIsArray($result);
}
public function teststates()
{
$servuceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$servuceMock->expects($this->atLeastOnce())
->method('getStates')
->will($this->returnValue(array()));
$this->api->setService($servuceMock);
$result = $this->api->states();
$this->assertIsArray($result);
}
public function testcountries_eunion()
{
$servuceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$servuceMock->expects($this->atLeastOnce())
->method('getEuCountries')
->will($this->returnValue(array()));
$this->api->setService($servuceMock);
$result = $this->api->countries_eunion();
$this->assertIsArray($result);
}
public function testcountries()
{
$servuceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$servuceMock->expects($this->atLeastOnce())
->method('getCountries')
->will($this->returnValue(array()));
$this->api->setService($servuceMock);
$result = $this->api->countries();
$this->assertIsArray($result);
}
public function testPeriods()
{
$result = $this->api->periods();
$this->assertIsArray($result);
}
public function testperiod_title()
{
$data = array('code' => 'periodCode');
$servuceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$servuceMock->expects($this->atLeastOnce())
->method('getPeriod')
->will($this->returnValue('periodTtitleValue'));
$di = new \Pimple\Container();
$this->api->setDi($di);
$this->api->setService($servuceMock);
$result = $this->api->period_title($data);
$this->assertIsString($result);
}
public function testperiod_titleMissingCode()
{
$data = array();
$expected = '-';
$di = new \Pimple\Container();
$this->api->setDi($di);
$result = $this->api->period_title($data);
$this->assertIsString($result);
$this->assertEquals($expected, $result);
}
public function testtemplate_exists()
{
$data = array(
'file' => 'testing.txt',
);
$servuceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$servuceMock->expects($this->atLeastOnce())
->method('templateExists')
->will($this->returnValue(true));
$this->api->setService($servuceMock);
$result = $this->api->template_exists($data);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testtemplate_existsFileParamMissing()
{
$data = array(
);
$result = $this->api->template_exists($data);
$this->assertIsBool($result);
$this->assertFalse($result);
}
public function testlocale()
{
$setLang = 'en_US';
$di = new \Pimple\Container();
$di['config'] = [ 'i18n' => ['locale' => 'en_US' ] ];
$this->api->setDi($di);
$result = $this->api->locale();
$this->assertIsString($result);
$this->assertNotEmpty($result);
$this->assertEquals($setLang, $result);
}
public function testget_pending_messages()
{
$serviceMock = $this->getMockBuilder('\Box\Mod\System\Service')->getMock();
$messageArr = array('Important message to user');
$serviceMock->expects($this->atLeastOnce())
->method('getPendingMessages')
->willReturn($messageArr);
$serviceMock->expects($this->atLeastOnce())
->method('clearPendingMessages');
$this->api->setService($serviceMock);
$result = $this->api->get_pending_messages();
$this->assertIsArray($result);
$this->assertEquals($messageArr, $result);
}
}