Completed
Push — master ( c434e9...dcf3b9 )
by Adrien
09:01
created

ProtectionTest::testVerifyPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 20
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 9.6
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
4
5
use PhpOffice\PhpSpreadsheet\Worksheet\Protection;
6
use PHPUnit\Framework\TestCase;
7
8
class ProtectionTest extends TestCase
9
{
10
    public function testVerifyPassword(): void
11
    {
12
        $protection = new Protection();
13
        self::assertTrue($protection->verify('foo'), 'non-protected always pass');
14
15
        $protection->setSheet(true);
16
        self::assertFalse($protection->verify('foo'), 'protected will fail');
17
18
        $protection->setPassword('foo', true);
19
        self::assertSame('foo', $protection->getPassword(), 'was not stored as-is, without hashing');
20
        self::assertFalse($protection->verify('foo'), 'setting already hashed password will not match');
21
22
        $protection->setPassword('foo');
23
        self::assertSame('CC40', $protection->getPassword(), 'was hashed');
24
        self::assertTrue($protection->verify('foo'), 'setting non-hashed password will hash it and not match');
25
26
        $protection->setAlgorithm(Protection::ALGORITHM_MD5);
27
        self::assertFalse($protection->verify('foo'), 'changing algorithm will not match anymore');
28
29
        $protection->setPassword('foo');
30
        $hash1 = $protection->getPassword();
31
        $protection->setPassword('foo');
32
        $hash2 = $protection->getPassword();
33
34
        self::assertSame(24, mb_strlen($hash1));
35
        self::assertSame(24, mb_strlen($hash2));
36
        self::assertNotSame($hash1, $hash2, 'was hashed with automatic salt');
37
        self::assertTrue($protection->verify('foo'), 'setting password again, will hash with proper algorithm and will match');
38
    }
39
}
40