123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- <?php
-
- require_once 'Swift/Tests/SwiftUnitTestCase.php';
- require_once 'Swift/Mime/ContentEncoder/QpContentEncoder.php';
- require_once 'Swift/InputByteStream.php';
- require_once 'Swift/OutputByteStream.php';
- require_once 'Swift/CharacterStream.php';
-
- class Swift_StreamCollector implements Yay_Action {
- public $content = '';
- public function &invoke(Yay_Invocation $inv) {
- $args = $inv->getArguments();
- $this->content .= current($args);
- }
- public function describeTo(Yay_Description $description) {
- $description->appendText(' gathers input;');
- }
- }
-
- class Swift_Mime_ContentEncoder_QpContentEncoderTest
- extends Swift_Tests_SwiftUnitTestCase
- {
-
- public function testNameIsQuotedPrintable()
- {
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder(
- $this->_createCharacterStream(true)
- );
- $this->assertEqual('quoted-printable', $encoder->getName());
- }
-
-
-
-
- public function testPermittedCharactersAreNotEncoded()
- {
-
-
-
- foreach (array_merge(range(33, 60), range(62, 126)) as $ordinal)
- {
- $char = chr($ordinal);
-
- $os = $this->_createOutputByteStream(true);
- $charStream = $this->_createCharacterStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> one($charStream)->flushContents()
- -> one($charStream)->importByteStream($os)
- -> one($charStream)->readBytes(any()) -> returns(array($ordinal))
- -> allowing($charStream)->readBytes(any()) -> returns(false)
-
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> ignoring($os)
- );
-
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
- $encoder->encodeByteStream($os, $is);
- $this->assertIdenticalBinary($char, $collection->content);
- }
- }
-
- public function testLinearWhiteSpaceAtLineEndingIsEncoded()
- {
-
-
-
- $HT = chr(0x09);
- $SPACE = chr(0x20);
-
-
- $os = $this->_createOutputByteStream(true);
- $charStream = $this->_createCharacterStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> one($charStream)->flushContents()
- -> one($charStream)->importByteStream($os)
- -> one($charStream)->readBytes(any()) -> returns(array(ord('a')))
- -> one($charStream)->readBytes(any()) -> returns(array(0x09))
- -> one($charStream)->readBytes(any()) -> returns(array(0x09))
- -> one($charStream)->readBytes(any()) -> returns(array(0x0D))
- -> one($charStream)->readBytes(any()) -> returns(array(0x0A))
- -> one($charStream)->readBytes(any()) -> returns(array(ord('b')))
- -> allowing($charStream)->readBytes(any()) -> returns(false)
-
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> ignoring($os)
- );
-
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
- $encoder->encodeByteStream($os, $is);
-
- $this->assertEqual("a\t=09\r\nb", $collection->content);
-
-
- $os = $this->_createOutputByteStream(true);
- $charStream = $this->_createCharacterStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> one($charStream)->flushContents()
- -> one($charStream)->importByteStream($os)
- -> one($charStream)->readBytes(any()) -> returns(array(ord('a')))
- -> one($charStream)->readBytes(any()) -> returns(array(0x20))
- -> one($charStream)->readBytes(any()) -> returns(array(0x20))
- -> one($charStream)->readBytes(any()) -> returns(array(0x0D))
- -> one($charStream)->readBytes(any()) -> returns(array(0x0A))
- -> one($charStream)->readBytes(any()) -> returns(array(ord('b')))
- -> allowing($charStream)->readBytes(any()) -> returns(false)
-
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> ignoring($os)
- );
-
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
- $encoder->encodeByteStream($os, $is);
-
- $this->assertEqual("a =20\r\nb", $collection->content);
- }
-
- public function testCRLFIsLeftAlone()
- {
-
-
-
- $os = $this->_createOutputByteStream(true);
- $charStream = $this->_createCharacterStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> one($charStream)->flushContents()
- -> one($charStream)->importByteStream($os)
- -> one($charStream)->readBytes(any()) -> returns(array(ord('a')))
- -> one($charStream)->readBytes(any()) -> returns(array(0x0D))
- -> one($charStream)->readBytes(any()) -> returns(array(0x0A))
- -> one($charStream)->readBytes(any()) -> returns(array(ord('b')))
- -> one($charStream)->readBytes(any()) -> returns(array(0x0D))
- -> one($charStream)->readBytes(any()) -> returns(array(0x0A))
- -> one($charStream)->readBytes(any()) -> returns(array(ord('c')))
- -> one($charStream)->readBytes(any()) -> returns(array(0x0D))
- -> one($charStream)->readBytes(any()) -> returns(array(0x0A))
- -> allowing($charStream)->readBytes(any()) -> returns(false)
-
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> ignoring($os)
- );
-
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
- $encoder->encodeByteStream($os, $is);
- $this->assertEqual("a\r\nb\r\nc\r\n", $collection->content);
- }
-
- public function testLinesLongerThan76CharactersAreSoftBroken()
- {
-
-
-
- $os = $this->_createOutputByteStream(true);
- $charStream = $this->_createCharacterStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> one($charStream)->flushContents()
- -> one($charStream)->importByteStream($os)
-
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> ignoring($os)
- );
-
- for ($seq = 0; $seq <= 140; ++$seq)
- {
- $this->_checking(Expectations::create()
- -> one($charStream)->readBytes(any()) -> returns(array(ord('a')))
- );
- }
- $this->_checking(Expectations::create()
- -> allowing($charStream)->readBytes(any()) -> returns(false)
- );
-
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
- $encoder->encodeByteStream($os, $is);
- $this->assertEqual(str_repeat('a', 75) . "=\r\n" . str_repeat('a', 66), $collection->content);
- }
-
- public function testMaxLineLengthCanBeSpecified()
- {
- $os = $this->_createOutputByteStream(true);
- $charStream = $this->_createCharacterStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> one($charStream)->flushContents()
- -> one($charStream)->importByteStream($os)
-
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> ignoring($os)
- );
-
-
- for ($seq = 0; $seq <= 100; ++$seq)
- {
- $this->_checking(Expectations::create()
- -> one($charStream)->readBytes(any()) -> returns(array(ord('a')))
- );
- }
-
- $this->_checking(Expectations::create()
- -> one($charStream)->readBytes(any()) -> returns(false)
- );
-
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
- $encoder->encodeByteStream($os, $is, 0, 54);
- $this->assertEqual(str_repeat('a', 53) . "=\r\n" . str_repeat('a', 48), $collection->content);
- }
-
- public function testBytesBelowPermittedRangeAreEncoded()
- {
-
-
-
- foreach (range(0, 32) as $ordinal)
- {
- $char = chr($ordinal);
-
- $os = $this->_createOutputByteStream(true);
- $charStream = $this->_createCharacterStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> one($charStream)->flushContents()
- -> one($charStream)->importByteStream($os)
-
- -> one($charStream)->readBytes(any()) -> returns(array($ordinal))
- -> allowing($charStream)->readBytes(any()) -> returns(false)
-
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> ignoring($os)
- );
-
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
- $encoder->encodeByteStream($os, $is);
- $this->assertEqual(sprintf('=%02X', $ordinal), $collection->content);
- }
- }
-
- public function testDecimalByte61IsEncoded()
- {
-
-
-
- $char = chr(61);
-
- $os = $this->_createOutputByteStream(true);
- $charStream = $this->_createCharacterStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> one($charStream)->flushContents()
- -> one($charStream)->importByteStream($os)
-
- -> one($charStream)->readBytes(any()) -> returns(array(61))
- -> allowing($charStream)->readBytes(any()) -> returns(false)
-
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> ignoring($os)
- );
-
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
- $encoder->encodeByteStream($os, $is);
- $this->assertEqual(sprintf('=%02X', 61), $collection->content);
- }
-
- public function testBytesAbovePermittedRangeAreEncoded()
- {
-
-
-
- foreach (range(127, 255) as $ordinal)
- {
- $char = chr($ordinal);
-
- $os = $this->_createOutputByteStream(true);
- $charStream = $this->_createCharacterStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> one($charStream)->flushContents()
- -> one($charStream)->importByteStream($os)
-
- -> one($charStream)->readBytes(any()) -> returns(array($ordinal))
- -> allowing($charStream)->readBytes(any()) -> returns(false)
-
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> ignoring($os)
- );
-
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
- $encoder->encodeByteStream($os, $is);
- $this->assertEqual(sprintf('=%02X', $ordinal), $collection->content);
- }
- }
-
- public function testFirstLineLengthCanBeDifferent()
- {
- $os = $this->_createOutputByteStream(true);
- $charStream = $this->_createCharacterStream();
- $is = $this->_createInputByteStream();
- $collection = new Swift_StreamCollector();
-
- $this->_checking(Expectations::create()
- -> one($charStream)->flushContents()
- -> one($charStream)->importByteStream($os)
-
- -> allowing($is)->write(any(), optional()) -> will($collection)
- -> ignoring($is)
-
- -> ignoring($os)
- );
-
- for ($seq = 0; $seq <= 140; ++$seq)
- {
- $this->_checking(Expectations::create()
- -> one($charStream)->readBytes(any()) -> returns(array(ord('a')))
- );
- }
- $this->_checking(Expectations::create()
- -> one($charStream)->readBytes(any()) -> returns(false)
- );
-
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($charStream);
- $encoder->encodeByteStream($os, $is, 22);
- $this->assertEqual(
- str_repeat('a', 53) . "=\r\n" . str_repeat('a', 75) . "=\r\n" . str_repeat('a', 13),
- $collection->content
- );
- }
-
- public function testObserverInterfaceCanChangeCharset()
- {
- $stream = $this->_createCharacterStream();
- $this->_checking(Expectations::create()
- -> one($stream)->setCharacterSet('windows-1252')
- -> ignoring($stream)
- );
- $encoder = new Swift_Mime_ContentEncoder_QpContentEncoder($stream);
- $encoder->charsetChanged('windows-1252');
- }
-
-
-
- private function _createCharacterStream($stub = false)
- {
- return $stub
- ? $this->_stub('Swift_CharacterStream')
- : $this->_mock('Swift_CharacterStream')
- ;
- }
-
- private function _createEncoder($charStream)
- {
- return new Swift_Mime_HeaderEncoder_QpHeaderEncoder($charStream);
- }
-
- private function _createOutputByteStream($stub = false)
- {
- return $stub
- ? $this->_stub('Swift_OutputByteStream')
- : $this->_mock('Swift_OutputByteStream')
- ;
- }
-
- private function _createInputByteStream($stub = false)
- {
- return $stub
- ? $this->_stub('Swift_InputByteStream')
- : $this->_mock('Swift_InputByteStream')
- ;
- }
-
- }
|