Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
phpbb_random_gen_rand_string_test
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 setUp
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 test_gen_rand_string
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 test_gen_rand_string_friendly
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14class phpbb_random_gen_rand_string_test extends phpbb_test_case
15{
16    const TEST_COUNT = 100;
17    const MIN_STRING_LENGTH = 1;
18    const MAX_STRING_LENGTH = 15;
19
20    protected function setUp(): void
21    {
22        global $config;
23
24        if (!is_array($config))
25        {
26            $config = array();
27        }
28
29        $config['rand_seed'] = '';
30        $config['rand_seed_last_update'] = time() + 600;
31    }
32
33    public function test_gen_rand_string()
34    {
35        for ($tests = 0; $tests <= self::TEST_COUNT; ++$tests)
36        {
37            for ($num_chars = self::MIN_STRING_LENGTH; $num_chars <= self::MAX_STRING_LENGTH; ++$num_chars)
38            {
39                $random_string = gen_rand_string($num_chars);
40                $random_string_length = strlen($random_string);
41
42                $this->assertTrue($random_string_length >= self::MIN_STRING_LENGTH);
43                $this->assertTrue(
44                    $random_string_length == $num_chars,
45                    sprintf('Failed asserting that random string length matches expected length. Expected %1$u, Actual %2$u', $num_chars, $random_string_length)
46                );
47                $this->assertMatchesRegularExpression('#^[A-Z0-9]+$#', $random_string);
48            }
49        }
50    }
51
52    public function test_gen_rand_string_friendly()
53    {
54        for ($tests = 0; $tests <= self::TEST_COUNT; ++$tests)
55        {
56            for ($num_chars = self::MIN_STRING_LENGTH; $num_chars <= self::MAX_STRING_LENGTH; ++$num_chars)
57            {
58                $random_string = gen_rand_string_friendly($num_chars);
59                $random_string_length = strlen($random_string);
60
61                $this->assertTrue($random_string_length >= self::MIN_STRING_LENGTH);
62                $this->assertTrue(
63                    $random_string_length == $num_chars,
64                    sprintf('Failed asserting that random string length matches expected length. Expected %1$u, Actual %2$u', $num_chars, $random_string_length)
65                );
66                $this->assertMatchesRegularExpression('#^[A-NP-Z1-9]+$#', $random_string);
67            }
68        }
69    }
70}