1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5/**
6 * User id class test cases for the Crypt_GPG package.
7 *
8 * These tests require the PHPUnit 3.6 or greater package to be installed.
9 * PHPUnit is installable using PEAR. See the
10 * {@link http://www.phpunit.de/manual/3.6/en/installation.html manual}
11 * for detailed installation instructions.
12 *
13 * To run these tests, use:
14 * <code>
15 * $ phpunit UserIdTestCase
16 * </code>
17 *
18 * LICENSE:
19 *
20 * This library is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU Lesser General Public License as
22 * published by the Free Software Foundation; either version 2.1 of the
23 * License, or (at your option) any later version.
24 *
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, see
32 * <http://www.gnu.org/licenses/>
33 *
34 * @category  Encryption
35 * @package   Crypt_GPG
36 * @author    Michael Gauthier <mike@silverorange.com>
37 * @copyright 2008-2010 silverorange
38 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
39 * @version   CVS: $Id$
40 * @link      http://pear.php.net/package/Crypt_GPG
41 */
42
43/**
44 * Base test case.
45 */
46require_once 'TestCase.php';
47
48/**
49 * User Id class.
50 */
51require_once 'Crypt/GPG/UserId.php';
52
53/**
54 * User id class tests for Crypt_GPG.
55 *
56 * @category  Encryption
57 * @package   Crypt_GPG
58 * @author    Michael Gauthier <mike@silverorange.com>
59 * @copyright 2008-2010 silverorange
60 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
61 * @link      http://pear.php.net/package/Crypt_GPG
62 */
63class UserIdTest extends Crypt_GPG_TestCase
64{
65    /**
66     * @group construct
67     */
68    public function testConstructFromString()
69    {
70        $expectedUserId = new Crypt_GPG_UserId(
71            array(
72                'name'    => 'Example User',
73                'comment' => 'This is a test comment',
74                'email'   => 'test@example.com'
75            )
76        );
77
78        $string = 'Example User (This is a test comment) <test@example.com>';
79        $userId = new Crypt_GPG_UserId($string);
80
81        $this->assertEquals($expectedUserId, $userId);
82    }
83
84    /**
85     * @group construct
86     */
87    public function testConstructFromUserId()
88    {
89        $expectedUserId = new Crypt_GPG_UserId(
90            array(
91                'name'    => 'Example User',
92                'comment' => 'This is a test comment',
93                'email'   => 'test@example.com',
94                'revoked' => true,
95                'valid'   => false
96            )
97        );
98
99        $userId = new Crypt_GPG_UserId($expectedUserId);
100
101        $this->assertEquals($expectedUserId, $userId);
102    }
103
104    /**
105     * @group construct
106     */
107    public function testConstructFromArray()
108    {
109        $userId = new Crypt_GPG_UserId(
110            array(
111                'name'    => 'Example User',
112                'comment' => 'This is a test comment',
113                'email'   => 'test@example.com',
114                'revoked' => true,
115                'valid'   => false
116            )
117        );
118
119        $this->assertEquals('Example User', $userId->getName());
120        $this->assertEquals('This is a test comment', $userId->getComment());
121        $this->assertEquals('test@example.com', $userId->getEmail());
122
123        $this->assertTrue($userId->isRevoked());
124        $this->assertFalse($userId->isValid());
125    }
126
127    /**
128     * @group parse
129     */
130    public function testParseFull()
131    {
132        $expectedUserId = new Crypt_GPG_UserId(
133            array(
134                'name'    => 'Example User',
135                'comment' => 'This is a test comment',
136                'email'   => 'test@example.com'
137            )
138        );
139
140        $string = 'Example User (This is a test comment) <test@example.com>';
141        $userId = Crypt_GPG_UserId::parse($string);
142
143        $this->assertEquals($expectedUserId, $userId);
144    }
145
146    /**
147     * @group parse
148     */
149    public function testParseNameOnly()
150    {
151        $expectedUserId = new Crypt_GPG_UserId(array('name' => 'Example User'));
152
153        $string = 'Example User';
154        $userId = Crypt_GPG_UserId::parse($string);
155
156        $this->assertEquals($expectedUserId, $userId);
157    }
158
159    /**
160     * @group parse
161     */
162    public function testParseNameComment()
163    {
164        $expectedUserId = new Crypt_GPG_UserId(
165            array(
166                'name'    => 'Example User',
167                'comment' => 'This is a test comment'
168            )
169        );
170
171        $string = 'Example User (This is a test comment)';
172        $userId = Crypt_GPG_UserId::parse($string);
173
174        $this->assertEquals($expectedUserId, $userId);
175    }
176
177    /**
178     * @group parse
179     */
180    public function testParseNameEmail()
181    {
182        $expectedUserId = new Crypt_GPG_UserId(
183            array(
184                'name'  => 'Example User',
185                'email' => 'test@example.com'
186            )
187        );
188
189        $string = 'Example User <test@example.com>';
190        $userId = Crypt_GPG_UserId::parse($string);
191
192        $this->assertEquals($expectedUserId, $userId);
193    }
194
195    /**
196     * @group parse
197     */
198    public function testParseEmailOnly()
199    {
200        $expectedUserId = new Crypt_GPG_UserId(
201            array(
202                'name'  => '',
203                'email' => 'test@example.com'
204            )
205        );
206
207        $string = '<test@example.com>';
208        $userId = Crypt_GPG_UserId::parse($string);
209
210        $this->assertEquals($expectedUserId, $userId);
211
212        $string = 'test@example.com';
213        $userId = Crypt_GPG_UserId::parse($string);
214
215        $this->assertEquals($expectedUserId, $userId);
216    }
217
218    /**
219     * @group to-string
220     */
221    public function testToStringFull()
222    {
223        $expected = 'Example User (This is a test comment) <test@example.com>';
224
225        $userId = new Crypt_GPG_UserId(
226            array(
227                'name'    => 'Example User',
228                'comment' => 'This is a test comment',
229                'email'   => 'test@example.com'
230            )
231        );
232
233        $this->assertEquals($expected, strval($userId));
234    }
235
236    /**
237     * @group to-string
238     */
239    public function testToStringNameOnly()
240    {
241        $expected = 'Example User';
242
243        $userId = new Crypt_GPG_UserId(array('name' => 'Example User'));
244
245        $this->assertEquals($expected, strval($userId));
246    }
247
248    /**
249     * @group to-string
250     */
251    public function testToStringNameComment()
252    {
253        $expected = 'Example User (This is a test comment)';
254
255        $userId = new Crypt_GPG_UserId(
256            array(
257                'name'    => 'Example User',
258                'comment' => 'This is a test comment',
259            )
260        );
261
262        $this->assertEquals($expected, strval($userId));
263    }
264
265    /**
266     * @group to-string
267     */
268    public function testToStringNameEmail()
269    {
270        $expected = 'Example User <test@example.com>';
271
272        $userId = new Crypt_GPG_UserId(
273            array(
274                'name'  => 'Example User',
275                'email' => 'test@example.com'
276            )
277        );
278
279        $this->assertEquals($expected, strval($userId));
280    }
281
282    /**
283     * @group accessors
284     */
285    public function testGetName()
286    {
287        $userId = new Crypt_GPG_UserId(
288            array(
289                'name' => 'Example User'
290            )
291        );
292
293        $this->assertEquals('Example User', $userId->getName());
294    }
295
296    /**
297     * @group accessors
298     */
299    public function testGetComment()
300    {
301        $userId = new Crypt_GPG_UserId(
302            array(
303                'name'    => 'Example User',
304                'comment' => 'This is a test comment'
305            )
306        );
307
308        $this->assertEquals('This is a test comment', $userId->getComment());
309    }
310
311    /**
312     * @group accessors
313     */
314    public function testGetEmail()
315    {
316        $userId = new Crypt_GPG_UserId(
317            array(
318                'name'  => 'Example User',
319                'email' => 'test@example.com'
320            )
321        );
322
323        $this->assertEquals('test@example.com', $userId->getEmail());
324    }
325
326    /**
327     * @group accessors
328     */
329    public function testIsRevoked()
330    {
331        $userId = new Crypt_GPG_UserId(
332            array(
333                'name'    => 'Example User',
334                'revoked' => true,
335            )
336        );
337
338        $this->assertTrue($userId->isRevoked());
339
340        $userId = new Crypt_GPG_UserId(
341            array(
342                'name'    => 'Example User',
343                'revoked' => false,
344            )
345        );
346
347        $this->assertFalse($userId->isRevoked());
348    }
349
350    /**
351     * @group accessors
352     */
353    public function testIsValid()
354    {
355        $userId = new Crypt_GPG_UserId(
356            array(
357                'name'  => 'Example User',
358                'valid' => true,
359            )
360        );
361
362        $this->assertTrue($userId->isValid());
363
364        $userId = new Crypt_GPG_UserId(
365            array(
366                'name'  => 'Example User',
367                'valid' => false,
368            )
369        );
370
371        $this->assertFalse($userId->isValid());
372    }
373
374    /**
375     * @group mutators
376     */
377    public function testSetName()
378    {
379        $expectedUserId = new Crypt_GPG_UserId(array('name' => 'Second Name'));
380        $userId         = new Crypt_GPG_UserId(array('name' => 'First Name'));
381
382        $userId->setName('Second Name');
383
384        $this->assertEquals($expectedUserId, $userId);
385    }
386
387    /**
388     * @group mutators
389     */
390    public function testSetComment()
391    {
392        $expectedUserId = new Crypt_GPG_UserId(
393            array(
394                'name'    => 'Example User',
395                'comment' => 'Second comment text'
396            )
397        );
398
399        $userId = new Crypt_GPG_UserId(
400            array(
401                'name'    => 'Example User',
402                'comment' => 'First comment text'
403            )
404        );
405
406        $userId->setComment('Second comment text');
407
408        $this->assertEquals($expectedUserId, $userId);
409    }
410
411    /**
412     * @group mutators
413     */
414    public function testSetEmail()
415    {
416        $expectedUserId = new Crypt_GPG_UserId(
417            array(
418                'name'  => 'Example User',
419                'email' => 'second@example.com'
420            )
421        );
422
423        $userId = new Crypt_GPG_UserId(
424            array(
425                'name'  => 'Example User',
426                'email' => 'first@example.com'
427            )
428        );
429
430        $userId->setEmail('second@example.com');
431
432        $this->assertEquals($expectedUserId, $userId);
433    }
434
435    /**
436     * @group mutators
437     */
438    public function testSetRevoked()
439    {
440        $expectedUserId = new Crypt_GPG_UserId(
441            array(
442                'name'    => 'Example User',
443                'revoked' => true,
444            )
445        );
446
447        $userId = new Crypt_GPG_UserId(
448            array(
449                'name'    => 'Example User',
450                'revoked' => false,
451            )
452        );
453
454        $userId->setRevoked(true);
455
456        $this->assertEquals($expectedUserId, $userId);
457    }
458
459    /**
460     * @group mutators
461     */
462    public function testSetValid()
463    {
464        $expectedUserId = new Crypt_GPG_UserId(
465            array(
466                'name'  => 'Example User',
467                'valid' => true,
468            )
469        );
470
471        $userId = new Crypt_GPG_UserId(
472            array(
473                'name'  => 'Example User',
474                'valid' => false,
475            )
476        );
477
478        $userId->setValid(true);
479
480        $this->assertEquals($expectedUserId, $userId);
481    }
482
483    /**
484     * @group fluent
485     */
486    public function testFluentInterface()
487    {
488        $userId         = new Crypt_GPG_UserId();
489        $returnedUserId = $userId->setName('Alice');
490
491        $this->assertEquals(
492            $userId,
493            $returnedUserId,
494            'Failed asserting fluent interface works for setName() method.'
495        );
496
497        $userId         = new Crypt_GPG_UserId();
498        $returnedUserId = $userId->setComment('encryption is fun');
499
500        $this->assertEquals(
501            $userId,
502            $returnedUserId,
503            'Failed asserting fluent interface works for setComment() method.'
504        );
505
506        $userId         = new Crypt_GPG_UserId();
507        $returnedUserId = $userId->setEmail('test@example.com');
508
509        $this->assertEquals(
510            $userId,
511            $returnedUserId,
512            'Failed asserting fluent interface works for setEmail() method.'
513        );
514
515        $userId         = new Crypt_GPG_UserId();
516        $returnedUserId = $userId->setRevoked(true);
517
518        $this->assertEquals(
519            $userId,
520            $returnedUserId,
521            'Failed asserting fluent interface works for setRevoked() method.'
522        );
523
524        $userId         = new Crypt_GPG_UserId();
525        $returnedUserId = $userId->setValid(true);
526
527        $this->assertEquals(
528            $userId,
529            $returnedUserId,
530            'Failed asserting fluent interface works for setValid() method.'
531        );
532    }
533}
534