Skip to content

Commit 7624663

Browse files
committed
add test
1 parent 0221414 commit 7624663

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/PhpWord/Writer/Word2007/Part/Settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ private function getPasswordHash($protection)
282282
// build low-order word and hig-order word and combine them
283283
$combinedKey = $this->buildCombinedKey($byteChars);
284284
// build reversed hexadecimal string
285-
$hex = strtoupper(dechex($combinedKey & 0xFFFFFFFF));
285+
$hex = str_pad(strtoupper(dechex($combinedKey & 0xFFFFFFFF)), 8, '0', \STR_PAD_LEFT);
286286
$reversedHex = $hex[6] . $hex[7] . $hex[4] . $hex[5] . $hex[2] . $hex[3] . $hex[0] . $hex[1];
287287

288288
$generatedKey = mb_convert_encoding($reversedHex, 'UCS-2LE', 'UTF-8');

tests/PhpWord/Writer/Word2007/Part/SettingsTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ public function testDocumentProtection()
5050
$this->assertTrue($doc->elementExists($path, $file));
5151
}
5252

53+
/**
54+
* Test document protection with password
55+
*
56+
* Note: to get comparison values, a docx was generated in Word2010 and the values taken from the settings.xml
57+
*/
58+
public function testDocumentProtectionWithPassword()
59+
{
60+
$phpWord = new PhpWord();
61+
$phpWord->getProtection()->setEditing('readOnly');
62+
$phpWord->getProtection()->setPassword('testÄö@€!$&');
63+
$phpWord->getProtection()->setSalt(base64_decode("uq81pJRRGFIY5U+E9gt8tA=="));
64+
65+
$doc = TestHelperDOCX::getDocument($phpWord);
66+
67+
$file = 'word/settings.xml';
68+
69+
$path = '/w:settings/w:documentProtection';
70+
$this->assertTrue($doc->elementExists($path, $file));
71+
$this->assertEquals($doc->getElement($path, $file)->getAttribute('w:hash'), "RA9jfY/u3DX114PMcl+uSekxsYk=");
72+
}
73+
5374
/**
5475
* Test compatibility
5576
*/

0 commit comments

Comments
 (0)