From b46760fe5cc5492a7ebe1196f98af483f15eea8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Skrzypczak?= Date: Thu, 2 Nov 2023 05:53:26 +0100 Subject: [PATCH] Improved tests --- tests/App/Purifier.php | 58 +++++++++++++-------------- tests/Base/Z_ResetingRecordNumber.php | 39 +++++------------- 2 files changed, 39 insertions(+), 58 deletions(-) diff --git a/tests/App/Purifier.php b/tests/App/Purifier.php index 9c24c0f1de6b..0cb7c9a01d59 100644 --- a/tests/App/Purifier.php +++ b/tests/App/Purifier.php @@ -85,7 +85,28 @@ public static function setUpBeforeClass(): void $userRecordModel->set('hour_format', '24'); $userRecordModel->set('time_zone', \App\Fields\DateTime::getTimeZone()); $userRecordModel->save(); - \date_default_timezone_set(\App\Fields\DateTime::getTimeZone()); + date_default_timezone_set(\App\Fields\DateTime::getTimeZone()); + } + + /** + * Restore current user preferences. + * + * @throws \Exception + */ + public static function tearDownAfterClass(): void + { + $userModel = \Vtiger_Record_Model::getInstanceById(\App\User::getCurrentUserId(), 'Users'); + $userModel->set('currency_decimal_separator', self::$separatorDecimal); + $userModel->set('currency_grouping_separator', self::$separatorGrouping); + $userModel->set('currency_symbol_placement', self::$symbolPlacement); + $userModel->set('currency_grouping_pattern', self::$patternGrouping); + $userModel->set('no_of_currency_decimals', self::$decimalNum); + $userModel->set('truncate_trailing_zeros', self::$truncateTrailingZeros); + $userModel->set('hour_format', self::$hourFormat); + $userModel->set('time_zone', self::$userTimeZone); + $userModel->save(); + date_default_timezone_set(self::$timeZone); + parent::tearDownAfterClass(); } /** @@ -97,7 +118,7 @@ public static function setUpBeforeClass(): void */ public function dataProviderByType() { - //$type, $assertion, $expected, $text, $message, $exception + // $type, $assertion, $expected, $text, $message, $exception return [ ['Standard', 'Same', 'Test-text-string-for-purifier', 'Test-text-string-for-purifier', 'Sample text should be unchanged', null], ['Standard', 'Same', ['Test-text-string-for-purifier', 'Test-text-string-for-purifier'], ['Test-text-string-for-purifier', 'Test-text-string-for-purifier'], 'Sample text should be unchanged(array)', null], @@ -108,8 +129,8 @@ public function dataProviderByType() [2, 'NotSame', 'Test_text_alnum_4_purifier%$54#T$#BR-', 'Test_text_alnum_4_purifier%$54#T$#BR-', 'Sample text should be purified', \App\Exceptions\IllegalValue::class], ['DateInUserFormat', 'Same', date('Y-m-d'), date('Y-m-d'), 'Sample text should be unchanged', null], ['DateInUserFormat', 'NotSame', date('Y.m.d'), date('Y.m.d'), 'Sample text should be purified', \App\Exceptions\IllegalValue::class], - ['DateRangeUserFormat', 'Same', [date('Y-m-d'), date('Y-m-d', \strtotime('+1 day'))], date('Y-m-d') . ',' . date('Y-m-d', \strtotime('+1 day')), 'Sample text should be unchanged', null], - ['DateRangeUserFormat', 'NotSame', date('Y.m.d') . ',' . date('Y.m.d', \strtotime('+1 day')), date('Y.m.d') . ',' . date('Y.m.d', \strtotime('+1 day')), 'Sample text should be purified', \App\Exceptions\IllegalValue::class], + ['DateRangeUserFormat', 'Same', [date('Y-m-d'), date('Y-m-d', strtotime('+1 day'))], date('Y-m-d') . ',' . date('Y-m-d', strtotime('+1 day')), 'Sample text should be unchanged', null], + ['DateRangeUserFormat', 'NotSame', date('Y.m.d') . ',' . date('Y.m.d', strtotime('+1 day')), date('Y.m.d') . ',' . date('Y.m.d', strtotime('+1 day')), 'Sample text should be purified', \App\Exceptions\IllegalValue::class], ['date', 'Same', date('Y-m-d'), date('Y-m-d'), 'Sample text should be unchanged', null], ['date', 'NotSame', '201X-07-26', '201X-07-26', 'Sample text should be purified', \App\Exceptions\IllegalValue::class], ['time', 'Same', date('H:i:s'), date('H:i:s'), 'Sample text should be unchanged', null], @@ -131,9 +152,9 @@ public function dataProviderByType() ['Text', 'NotSame', 'ę€ółśążźćń23{}":?>><>?:"{}+_)(*&^%$#@!) <svg/onabort=alert(3)// ><>?:"{}+_)(*&^%$#@!) <svg/onabort=alert(3)// ><>?:"{}+_)(*&^%$#@!) <svg/onabort=alert(3)// ><>?:"{}+_)(*&^%$#@!) <svg/onabort=alert(3)// (added by postmaster@cc.it)', '<5FB2B5EF@xx.cc.it> (added by postmaster@cc.it)', 'Sample text should be unchanged', null], - ['MailId', 'Same', '<30.123.12.JavaMail."admin.azure"@A-PROXY01>', '<30.123.12.JavaMail."admin.azure"@A-PROXY01>', 'Sample text should be unchanged', null], - ['MailId', 'Same', '', '', 'Sample text should be unchanged', null], + ['MailId', 'Same', '5FB2B5EF@xx.cc.it> (added by postmaster@cc.it)', '<5FB2B5EF@xx.cc.it> (added by postmaster@cc.it)', 'Sample text should be unchanged', null], + ['MailId', 'Same', '30.123.12.JavaMail."admin.azure"@A-PROXY01', '<30.123.12.JavaMail."admin.azure"@A-PROXY01>', 'Sample text should be unchanged', null], + ['MailId', 'Same', 'CAK01GN-UtTiM90_wQNB07OnE6aBm=w@mail.g.c', '', 'Sample text should be unchanged', null], [\App\Purifier::PATH, 'NotSame', '../Test', '../Test', 'Path should be discarded', \App\Exceptions\IllegalValue::class], [\App\Purifier::PATH, 'Same', '/Test/test', '/Test/test', 'Path should be unchanged', null], ]; @@ -246,7 +267,7 @@ public function purifyHtmlProviderSuccess(): array ['
Test-text-string-for-purifier
', '
Test-text-string-for-purifier
', true], ['ę€ółśążźćń23{}":?>><>?:"{}+_)', 'ę€ółśążźćń23{}":?>><>?:"{}+_)', true], ['ę€ółśążźćń23{}":?>><>?:"{}+_)(*&^%$#@!)', 'ę€ółśążźćń23{}":?>><>?:"{}+_)(*&^%$#@!)', true], - [\file_get_contents(ROOT_DIRECTORY . '/tests/data/phpFile1.html'), \file_get_contents(ROOT_DIRECTORY . '/tests/data/phpFile2.html'), true], + [file_get_contents(ROOT_DIRECTORY . '/tests/data/phpFile1.html'), file_get_contents(ROOT_DIRECTORY . '/tests/data/phpFile2.html'), true], ['

', '

', true], ]; } @@ -263,25 +284,4 @@ public function testPurifyHtmlSuccess(string $text, string $expected): void { $this->assertSame($expected, \App\Purifier::purifyHtml($text), 'Sample text should be unchanged'); } - - /** - * Restore current user preferences. - * - * @throws \Exception - */ - public static function tearDownAfterClass(): void - { - $userModel = \Vtiger_Record_Model::getInstanceById(\App\User::getCurrentUserId(), 'Users'); - $userModel->set('currency_decimal_separator', self::$separatorDecimal); - $userModel->set('currency_grouping_separator', self::$separatorGrouping); - $userModel->set('currency_symbol_placement', self::$symbolPlacement); - $userModel->set('currency_grouping_pattern', self::$patternGrouping); - $userModel->set('no_of_currency_decimals', self::$decimalNum); - $userModel->set('truncate_trailing_zeros', self::$truncateTrailingZeros); - $userModel->set('hour_format', self::$hourFormat); - $userModel->set('time_zone', self::$userTimeZone); - $userModel->save(); - \date_default_timezone_set(self::$timeZone); - parent::tearDownAfterClass(); - } } diff --git a/tests/Base/Z_ResetingRecordNumber.php b/tests/Base/Z_ResetingRecordNumber.php index b674ad318307..f60ddd8d51f4 100644 --- a/tests/Base/Z_ResetingRecordNumber.php +++ b/tests/Base/Z_ResetingRecordNumber.php @@ -84,6 +84,15 @@ public static function setUpBeforeClass(): void self::$transaction = \App\Db::getInstance()->beginTransaction(); } + /** + * Cleaning after tests. + */ + public static function tearDownAfterClass(): void + { + self::$transaction->rollBack(); + \App\Cache::clear(); + } + /** * Test method "DateMock". */ @@ -135,25 +144,6 @@ public function testIncrementNumberStandard() } } - /** - * Test method "Parse". - * Test parsing method for record numbers on different dates. - */ - public function testParse() - { - $instance = RecordNumber::getInstance('FInvoice'); - foreach (RecordNumber::$dates as $index => $date) { - RecordNumber::$currentDateIndex = $index; - $parts = explode('-', $date); - $instance->set('prefix', '{{DD}}/'); - $this->assertSame($parts[2] . '/1', $instance->parseNumber(1)); - $instance->set('prefix', '{{MM}}/'); - $this->assertSame($parts[1] . '/1', $instance->parseNumber(1)); - $instance->set('prefix', '{{YYYY}}/'); - $this->assertSame($parts[0] . '/1', $instance->parseNumber(1)); - } - } - /** * Test method "IncrementNumberDay". * Test record number resetting with new day. @@ -344,7 +334,7 @@ public function testLeadingZeros(): void $currentNumber = 1; $currentDate = $sequence; } - $currentNumber = \str_pad($currentNumber, $leadingZeros, '0', STR_PAD_LEFT); + $currentNumber = str_pad($currentNumber, $leadingZeros, '0', STR_PAD_LEFT); $this->assertSame("$date/$currentNumber", $instance->getIncrementNumber()); $number = RecordNumber::getInstance('FInvoice'); $this->assertSame($currentNumber + 1, $number->get('cur_id')); @@ -356,13 +346,4 @@ public function testLeadingZeros(): void } } } - - /** - * Cleaning after tests. - */ - public static function tearDownAfterClass(): void - { - self::$transaction->rollBack(); - \App\Cache::clear(); - } }