1<?php
2/**
3 * Copyright 2015-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @category   Horde
9 * @copyright  2015-2016 Horde LLC
10 * @license    http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 * @package    Imap_Client
12 * @subpackage UnitTests
13 */
14
15/**
16 * Base class for testing the Horde_Imap_Client_Data_Fetch object.
17 *
18 * @author     Michael Slusarz <slusarz@horde.org>
19 * @category   Horde
20 * @copyright  2015-2016 Horde LLC
21 * @ignore
22 * @license    http://www.horde.org/licenses/lgpl21 LGPL 2.1
23 * @package    Imap_Client
24 * @subpackage UnitTests
25 */
26abstract class Horde_Imap_Client_Data_Fetch_TestBase
27extends PHPUnit_Framework_TestCase
28{
29    private $ob;
30
31    /* Set in child class via _setUp(). */
32    protected $ob_class;
33    abstract protected function _setUp();
34
35    public function setUp()
36    {
37        $this->_setUp();
38
39        $this->ob = new Horde_Imap_Client_Data_Fetch($this->ob_class);
40    }
41
42    /**
43     * @dataProvider fullMsgProvider
44     */
45    public function testFullMsg($stream_ob, $set_stream)
46    {
47        $string = strval($stream_ob);
48        $stream_ob->rewind();
49
50        $this->ob->setFullMsg($set_stream ? $stream_ob->stream : $string);
51        $serialize_ob = unserialize(serialize($this->ob));
52
53        foreach (array($this->ob, $serialize_ob) as $val) {
54            $this->assertEquals(
55                $string,
56                $val->getFullMsg(false)
57            );
58            $this->assertEquals(
59                $string,
60                stream_get_contents($val->getFullMsg(true))
61            );
62        }
63    }
64
65    public function fullMsgProvider()
66    {
67        $stream = new Horde_Stream_String(array('string' => 'Foo'));
68
69        return array(
70            array(
71                clone $stream,
72                false
73            ),
74            array(
75                clone $stream,
76                true
77            )
78        );
79    }
80
81    public function testStructure()
82    {
83        $this->assertInstanceOf(
84            'Horde_Mime_Part',
85            $this->ob->getStructure()
86        );
87
88        $test = new Horde_Mime_Part();
89        $test->setType('image/foo');
90
91        $this->ob->setStructure($test);
92        $serialize_ob = unserialize(serialize($this->ob));
93
94        foreach (array($this->ob, $serialize_ob) as $val) {
95            $ret = $val->getStructure();
96
97            $this->assertInstanceOf(
98                'Horde_Mime_Part',
99                $ret
100            );
101            $this->assertEquals(
102                'image/foo',
103                $ret->getType('image/foo')
104            );
105        }
106    }
107
108    /**
109     * @dataProvider headersProvider
110     */
111    public function testHeaders($input, $string)
112    {
113        $label = 'foo';
114
115        if (!is_null($input)) {
116            $this->ob->setHeaders($label, $input);
117        }
118
119        $serialize_ob = unserialize(serialize($this->ob));
120
121        foreach (array($this->ob, $serialize_ob) as $val) {
122            $this->assertEquals(
123                $string,
124                $val->getHeaders($label)
125            );
126
127            $stream = $val->getHeaders(
128                $label,
129                constant($this->ob_class . '::HEADER_STREAM')
130            );
131            rewind($stream);
132
133            $this->assertEquals(
134                $string,
135                stream_get_contents($stream)
136            );
137
138            $hdr_ob = $val->getHeaders(
139                $label,
140                constant($this->ob_class . '::HEADER_PARSE')
141            );
142
143            $this->assertInstanceOf(
144                'Horde_Mime_Headers',
145                $hdr_ob
146            );
147            $this->assertEquals(
148                trim($string),
149                trim($hdr_ob->toString(array('nowrap' => true)))
150            );
151        }
152    }
153
154    public function headersProvider()
155    {
156        $hdrs = new Horde_Mime_Headers();
157        $hdrs->addHeader('From', 'test@example.com');
158
159        return array(
160            array(
161                null,
162                ''
163            ),
164            array(
165                "From: test@example.com\n\n",
166                "From: test@example.com\n\n"
167            ),
168            array(
169                $hdrs,
170                "From: test@example.com\n\n"
171            )
172        );
173    }
174
175    /**
176     * @dataProvider headerTextProvider
177     */
178    public function testHeaderText($input, $string)
179    {
180        $id = 1;
181
182        if (!is_null($input)) {
183            $this->ob->setHeaderText($id, $input);
184        }
185
186        $serialize_ob = unserialize(serialize($this->ob));
187
188        foreach (array($this->ob, $serialize_ob) as $val) {
189            $this->assertEquals(
190                $string,
191                $val->getHeaderText($id)
192            );
193
194            $stream = $val->getHeaderText(
195                $id,
196                constant($this->ob_class . '::HEADER_STREAM')
197            );
198            rewind($stream);
199
200            $this->assertEquals(
201                $string,
202                stream_get_contents($stream)
203            );
204
205            $hdr_ob = $val->getHeaderText(
206                $id,
207                constant($this->ob_class . '::HEADER_PARSE')
208            );
209
210            $this->assertInstanceOf(
211                'Horde_Mime_Headers',
212                $hdr_ob
213            );
214            $this->assertEquals(
215                trim($string),
216                trim($hdr_ob->toString(array('nowrap' => true)))
217            );
218        }
219    }
220
221    public function headerTextProvider()
222    {
223        return array(
224            array(
225                null,
226                ''
227            ),
228            array(
229                "From: test@example.com\n\n",
230                "From: test@example.com\n\n"
231            )
232        );
233    }
234
235    /**
236     * @dataProvider mimeHeaderProvider
237     */
238    public function testMimeHeader($input, $string)
239    {
240        $id = 1;
241
242        if (!is_null($input)) {
243            $this->ob->setMimeHeader($id, $input);
244        }
245
246        $serialize_ob = unserialize(serialize($this->ob));
247
248        foreach (array($this->ob, $serialize_ob) as $val) {
249            $this->assertEquals(
250                $string,
251                $val->getMimeHeader($id)
252            );
253
254            $stream = $val->getMimeHeader(
255                $id,
256                constant($this->ob_class . '::HEADER_STREAM')
257            );
258            rewind($stream);
259
260            $this->assertEquals(
261                $string,
262                stream_get_contents($stream)
263            );
264
265            $hdr_ob = $val->getMimeHeader(
266                $id,
267                constant($this->ob_class . '::HEADER_PARSE')
268            );
269
270            $this->assertInstanceOf(
271                'Horde_Mime_Headers',
272                $hdr_ob
273            );
274            $this->assertEquals(
275                trim($string),
276                trim($hdr_ob->toString(array('nowrap' => true)))
277            );
278        }
279    }
280
281    public function mimeHeaderProvider()
282    {
283        return array(
284            array(
285                null,
286                ''
287            ),
288            array(
289                "From: test@example.com\n\n",
290                "From: test@example.com\n\n"
291            )
292        );
293    }
294
295    /**
296     * @dataProvider bodyPartProvider
297     */
298    public function testBodyPart($stream_ob, $set_stream, $decode)
299    {
300        $id = 1;
301        $string = strval($stream_ob);
302        $stream_ob->rewind();
303
304        if (strlen($string)) {
305            $this->ob->setBodyPart(
306                $id,
307                $set_stream ? $stream_ob->stream : $string,
308                $decode
309            );
310        }
311
312        $serialize_ob = unserialize(serialize($this->ob));
313
314        foreach (array($this->ob, $serialize_ob) as $val) {
315            $this->assertEquals(
316                $string,
317                $val->getBodyPart($id, false)
318            );
319            $this->assertEquals(
320                $string,
321                stream_get_contents($val->getBodyPart($id, true))
322            );
323            $this->assertEquals(
324                $decode,
325                $this->ob->getBodyPartDecode($id)
326            );
327        }
328    }
329
330    public function bodyPartProvider()
331    {
332        $stream = new Horde_Stream_String(array('string' => 'Foo'));
333        $empty_stream = new Horde_Stream();
334
335        return array(
336            array(
337                clone $stream,
338                false,
339                null
340            ),
341            array(
342                clone $stream,
343                false,
344                '8bit'
345            ),
346            array(
347                clone $stream,
348                false,
349                'binary'
350            ),
351            array(
352                clone $empty_stream,
353                false,
354                null
355            ),
356            array(
357                clone $stream,
358                true,
359                null
360            ),
361            array(
362                clone $stream,
363                true,
364                '8bit'
365            ),
366            array(
367                clone $stream,
368                true,
369                'binary'
370            ),
371            array(
372                clone $empty_stream,
373                true,
374                null
375            )
376        );
377    }
378
379    /**
380     * @dataProvider bodyPartSizeProvider
381     */
382    public function testBodyPartSize($size)
383    {
384        $id = 1;
385
386        if (!is_null($size)) {
387            $this->ob->setBodyPartSize($id, $size);
388        }
389
390        $serialize_ob = unserialize(serialize($this->ob));
391
392        foreach (array($this->ob, $serialize_ob) as $val) {
393            $this->assertEquals(
394                $size,
395                $val->getBodyPartSize($id)
396            );
397        }
398
399    }
400
401    public function bodyPartSizeProvider()
402    {
403        return array(
404            array(200),
405            array(null)
406        );
407    }
408
409    /**
410     * @dataProvider bodyTextProvider
411     */
412    public function testBodyText($stream_ob, $set_stream)
413    {
414        $id = 1;
415        $string = strval($stream_ob);
416        $stream_ob->rewind();
417
418        if (strlen($string)) {
419            $this->ob->setBodyText(
420                $id,
421                $set_stream ? $stream_ob->stream : $string
422            );
423        }
424
425        $serialize_ob = unserialize(serialize($this->ob));
426
427        foreach (array($this->ob, $serialize_ob) as $val) {
428            $this->assertEquals(
429                $string,
430                $val->getBodyText($id, false)
431            );
432            $this->assertEquals(
433                $string,
434                stream_get_contents($val->getBodyText($id, true))
435            );
436        }
437    }
438
439    public function bodyTextProvider()
440    {
441        $stream = new Horde_Stream_String(array('string' => 'Foo'));
442
443        return array(
444            array(
445                clone $stream,
446                false
447            ),
448            array(
449                clone $stream,
450                true
451            )
452        );
453    }
454
455    /**
456     * @dataProvider envelopeProvider
457     */
458    public function testEnvelope($data, $to)
459    {
460        if (!is_null($data)) {
461            $this->ob->setEnvelope($data);
462        }
463
464        $serialize_ob = unserialize(serialize($this->ob));
465
466        foreach (array($this->ob, $serialize_ob) as $val) {
467            $envelope = $val->getEnvelope();
468
469            $this->assertInstanceOf(
470                'Horde_Imap_Client_Data_Envelope',
471                $envelope
472            );
473
474            $addr_ob = $envelope->to;
475
476            $this->assertInstanceof(
477                'Horde_Mail_Rfc822_List',
478                $addr_ob
479            );
480            $this->assertEquals(
481                $to,
482                strval($addr_ob)
483            );
484        }
485    }
486
487    public function envelopeProvider()
488    {
489        $addr = new Horde_Imap_Client_Data_Envelope();
490        $addr->to = 'foo@example.com';
491
492        return array(
493            array(
494                array('to' => 'foo@example.com'),
495                'foo@example.com'
496            ),
497            array(
498                $addr,
499                'foo@example.com'
500            ),
501            array(
502                null,
503                ''
504            )
505        );
506    }
507
508    /**
509     * @dataProvider flagsProvider
510     */
511    public function testFlags($flags, $expected)
512    {
513        $this->ob->setFlags($flags);
514        $serialize_ob = unserialize(serialize($this->ob));
515
516        foreach (array($this->ob, $serialize_ob) as $val) {
517            $f = $this->ob->getFlags();
518
519            $this->assertEquals(
520                $expected,
521                $f
522            );
523        }
524    }
525
526    public function flagsProvider()
527    {
528        return array(
529            array(
530                array('foo'),
531                array('foo')
532            ),
533            array(
534                array(),
535                array(),
536            ),
537            array(
538                array('FoO', 'BAR', '     baZ  '),
539                array('foo', 'bar', 'baz')
540            )
541        );
542    }
543
544    /**
545     * @dataProvider imapDateProvider
546     */
547    public function testImapDate($date, $expected)
548    {
549        if (!is_null($date)) {
550            $this->ob->setImapDate($date);
551        }
552
553        $serialize_ob = unserialize(serialize($this->ob));
554
555        foreach (array($this->ob, $serialize_ob) as $val) {
556            $d = $this->ob->getImapDate();
557
558            $this->assertInstanceOf(
559                'Horde_Imap_Client_DateTime',
560                $d
561            );
562
563            $this->assertEquals(
564                is_null($expected) ? time() : $expected,
565                intval(strval($d))
566            );
567        }
568    }
569
570    public function imapDateProvider()
571    {
572        return array(
573            array(
574                '12 Sep 2007 15:49:12 UT',
575                1189612152
576            ),
577            array(
578                new Horde_Imap_Client_DateTime('12 Sep 2007 15:49:12 UT'),
579                1189612152
580            ),
581            array(
582                null,
583                null
584            )
585        );
586    }
587
588    /**
589     * @dataProvider sizeProvider
590     */
591    public function testSize($size, $expected)
592    {
593        if (!is_null($size)) {
594            $this->ob->setSize($size);
595        }
596
597        $serialize_ob = unserialize(serialize($this->ob));
598
599        foreach (array($this->ob, $serialize_ob) as $val) {
600            $this->assertEquals(
601                $expected,
602                $this->ob->getSize()
603            );
604        }
605    }
606
607    public function sizeProvider()
608    {
609        return array(
610            array(
611                200,
612                200
613            ),
614            array(
615                null,
616                0
617            )
618        );
619    }
620
621    /**
622     * @dataProvider uidProvider
623     */
624    public function testUid($uid, $expected)
625    {
626        if (!is_null($uid)) {
627            $this->ob->setUid($uid);
628        }
629
630        $serialize_ob = unserialize(serialize($this->ob));
631
632        foreach (array($this->ob, $serialize_ob) as $val) {
633            $this->assertEquals(
634                $expected,
635                $this->ob->getUid()
636            );
637        }
638    }
639
640    public function uidProvider()
641    {
642        return array(
643            array(
644                200,
645                200
646            ),
647            array(
648                null,
649                null
650            )
651        );
652    }
653
654    /**
655     * @dataProvider seqProvider
656     */
657    public function testSeq($seq, $expected)
658    {
659        if (!is_null($seq)) {
660            $this->ob->setSeq($seq);
661        }
662
663        $serialize_ob = unserialize(serialize($this->ob));
664
665        foreach (array($this->ob, $serialize_ob) as $val) {
666            $this->assertEquals(
667                $expected,
668                $this->ob->getSeq()
669            );
670        }
671    }
672
673    public function seqProvider()
674    {
675        return array(
676            array(
677                200,
678                200
679            ),
680            array(
681                null,
682                null
683            )
684        );
685    }
686
687    /**
688     * @dataProvider modSeqProvider
689     */
690    public function testModSeq($modseq, $expected)
691    {
692        if (!is_null($modseq)) {
693            $this->ob->setModSeq($modseq);
694        }
695
696        $serialize_ob = unserialize(serialize($this->ob));
697
698        foreach (array($this->ob, $serialize_ob) as $val) {
699            $this->assertEquals(
700                $expected,
701                $this->ob->getModSeq()
702            );
703        }
704    }
705
706    public function modSeqProvider()
707    {
708        return array(
709            array(
710                200,
711                200
712            ),
713            array(
714                null,
715                null
716            )
717        );
718    }
719
720    /**
721     * @dataProvider downgradedProvider
722     */
723    public function testDowngraded($downgraded, $expected)
724    {
725        if (!is_null($downgraded)) {
726            $this->ob->setDowngraded($downgraded);
727        }
728
729        $serialize_ob = unserialize(serialize($this->ob));
730
731        foreach (array($this->ob, $serialize_ob) as $val) {
732            $this->assertEquals(
733                $expected,
734                $this->ob->isDowngraded()
735            );
736        }
737    }
738
739    public function downgradedProvider()
740    {
741        return array(
742            array(
743                true,
744                true
745            ),
746            array(
747                false,
748                false
749            ),
750            array(
751                null,
752                false
753            )
754        );
755    }
756
757    public function testMerge()
758    {
759        $this->ob->setUid(1);
760
761        $this->assertEquals(
762            1,
763            $this->ob->getUid()
764        );
765        $this->assertNull($this->ob->getSeq());
766
767        $ob2 = new Horde_Imap_Client_Data_Fetch($this->ob_class);
768        $ob2->setUid(2);
769        $ob2->setSeq(2);
770
771        $this->ob->merge($ob2);
772
773        $this->assertEquals(
774            2,
775            $this->ob->getUid()
776        );
777        $this->assertEquals(
778            2,
779            $this->ob->getSeq()
780        );
781    }
782
783    public function testObjectStateFunctions()
784    {
785        $this->assertEmpty($this->ob->getRawData());
786        $this->assertFalse($this->ob->exists(Horde_Imap_Client::FETCH_UID));
787        $this->assertFalse($this->ob->exists(Horde_Imap_Client::FETCH_SEQ));
788        $this->assertTrue($this->ob->isDefault());
789
790        $this->ob->setUid(1);
791
792        $this->assertNotEmpty($this->ob->getRawData());
793        $this->assertTrue($this->ob->exists(Horde_Imap_Client::FETCH_UID));
794        $this->assertFalse($this->ob->exists(Horde_Imap_Client::FETCH_SEQ));
795        $this->assertFalse($this->ob->isDefault());
796    }
797
798    public function testClone()
799    {
800        $stream = new Horde_Stream_String(array('string' => 'Foo'));
801        $stream->rewind();
802
803        $this->ob->setFullMsg($stream->stream);
804
805        $ob2 = clone $this->ob;
806
807        $this->ob->setFullMsg('Bar');
808
809        $this->assertEquals(
810            'Bar',
811            $this->ob->getFullMsg()
812        );
813        $this->assertEquals(
814            'Foo',
815            $ob2->getFullMsg()
816        );
817    }
818
819}
820