|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of PHPWord - A pure PHP library for reading and writing |
| 4 | + * word processing documents. |
| 5 | + * |
| 6 | + * PHPWord is free software distributed under the terms of the GNU Lesser |
| 7 | + * General Public License version 3 as published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * For the full copyright and license information, please read the LICENSE |
| 10 | + * file that was distributed with this source code. For the full list of |
| 11 | + * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
| 12 | + * |
| 13 | + * @see https://github.com/PHPOffice/PHPWord |
| 14 | + * @copyright 2010-2018 PHPWord contributors |
| 15 | + * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 16 | + */ |
| 17 | + |
| 18 | +namespace PhpOffice\PhpWord\Escaper; |
| 19 | + |
| 20 | +/** |
| 21 | + * Test class for PhpOffice\PhpWord\Escaper\RTF |
| 22 | + */ |
| 23 | +class RtfEscaper2Test extends \PHPUnit\Framework\TestCase |
| 24 | +{ |
| 25 | + const HEADER = '\\pard\\nowidctlpar {\\cf0\\f0 '; |
| 26 | + const TRAILER = '}\\par'; |
| 27 | + |
| 28 | + public function escapestring($str) |
| 29 | + { |
| 30 | + \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true); |
| 31 | + $parentWriter = new \PhpOffice\PhpWord\Writer\RTF(); |
| 32 | + $element = new \PhpOffice\PhpWord\Element\Text($str); |
| 33 | + $txt = new \PhpOffice\PhpWord\Writer\RTF\Element\Text($parentWriter, $element); |
| 34 | + $txt2 = trim($txt->write()); |
| 35 | + |
| 36 | + return $txt2; |
| 37 | + } |
| 38 | + |
| 39 | + public function expect($str) |
| 40 | + { |
| 41 | + return self::HEADER . $str . self::TRAILER; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test special characters which require escaping |
| 46 | + */ |
| 47 | + public function testSpecial() |
| 48 | + { |
| 49 | + $str = 'Special characters { open brace } close brace \\ backslash'; |
| 50 | + $expect = $this->expect('Special characters \\{ open brace \\} close brace \\\\ backslash'); |
| 51 | + $this->assertEquals($expect, $this->escapestring($str)); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Test accented character |
| 56 | + */ |
| 57 | + public function testAccent() |
| 58 | + { |
| 59 | + $str = 'Voilà - string with accented char'; |
| 60 | + $expect = $this->expect('Voil\\uc0{\\u224} - string with accented char'); |
| 61 | + $this->assertEquals($expect, $this->escapestring($str)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Test Hebrew |
| 66 | + */ |
| 67 | + public function testHebrew() |
| 68 | + { |
| 69 | + $str = 'Hebrew - שלום'; |
| 70 | + $expect = $this->expect('Hebrew - \\uc0{\\u1513}\\uc0{\\u1500}\\uc0{\\u1493}\\uc0{\\u1501}'); |
| 71 | + $this->assertEquals($expect, $this->escapestring($str)); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test tab |
| 76 | + */ |
| 77 | + public function testTab() |
| 78 | + { |
| 79 | + $str = "Here's a tab\tfollowed by more characters."; |
| 80 | + $expect = $this->expect("Here's a tab{\\tab}followed by more characters."); |
| 81 | + $this->assertEquals($expect, $this->escapestring($str)); |
| 82 | + } |
| 83 | +} |
0 commit comments