[5888] | 1 | <?php |
---|
| 2 | require_once(__DIR__ . '/../../../api/controller.php'); |
---|
| 3 | require_once(__DIR__ . '/../../../library/tonic/lib/tonic.php'); |
---|
| 4 | require_once(__DIR__ . '/../../../rest/mail/MailLastResource.php'); |
---|
| 5 | require_once(__DIR__ . '/../../../rest/oauth/OAuth2StorageUserCredential.php'); |
---|
| 6 | |
---|
| 7 | class MailLastResourceTest extends PHPUnit_Framework_TestCase { |
---|
| 8 | |
---|
| 9 | private $class = null; |
---|
| 10 | private $user = 'user2'; |
---|
| 11 | private $pass = 'prognus'; |
---|
| 12 | private $mbox = null; |
---|
| 13 | private $cyrusConf = null; |
---|
| 14 | private $cyrusOPTS = null; |
---|
| 15 | private $accessToken = 'aaaaaa259f553ac148f01b6bbcbb101'; |
---|
| 16 | private $refreshToken = 'rrrrrr03fff3b8cdc51206244529abd'; |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | private $client_id = 666; |
---|
| 20 | private $user_id = 666; |
---|
| 21 | |
---|
| 22 | //configura classe para testes |
---|
| 23 | public function setUp() |
---|
| 24 | { |
---|
| 25 | |
---|
| 26 | $request = new Request(); |
---|
| 27 | $this->class = new MailLastResource($request); |
---|
| 28 | $this->classOAuth = new OAuth2StorageUserCredential(); |
---|
| 29 | |
---|
| 30 | $_SESSION['wallet']['user']['uid'] = $this->user; |
---|
| 31 | $_SESSION['wallet']['user']['password'] = $this->pass; |
---|
| 32 | |
---|
| 33 | $this->cyrusConf = Config::service('Cyrus', 'config'); |
---|
| 34 | $this->cyrusOPTS = ($this->cyrusConf['tlsEncryption']) ? '/tls/novalidate-cert' : '/notls/novalidate-cert'; |
---|
| 35 | $this->mbox = imap_open( '{'.$this->cyrusConf['host'].":".$this->cyrusConf['port'].$this->cyrusOPTS.'}INBOX' , Config::me('uid') , Config::me('password') ); |
---|
| 36 | |
---|
| 37 | $messages = imap_sort( $this->mbox , SORTDATE , 1 , null , "UNSEEN" , 'UTF-8'); |
---|
| 38 | if (is_array($messages)) |
---|
| 39 | foreach ($messages as $msg_number) |
---|
| 40 | imap_delete($this->mbox, $msg_number); |
---|
| 41 | |
---|
| 42 | imap_expunge ( $this->mbox ); |
---|
| 43 | |
---|
| 44 | //insere access token |
---|
| 45 | $this->classOAuth->setAccessToken($this->accessToken, $this->client_id, $this->user_id, (time() + 3600), 'all', $this->refreshToken); |
---|
| 46 | |
---|
| 47 | //insere refresh token |
---|
| 48 | $this->classOAuth->setRefreshToken($this->refreshToken, $this->client_id, $this->user_id, (time() + 3600), 'all'); |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | } |
---|
| 52 | //finaliza classe |
---|
| 53 | public function tearDown() |
---|
| 54 | { |
---|
| 55 | $messages = imap_sort( $this->mbox , SORTDATE , 1 , null , "UNSEEN" , 'UTF-8'); |
---|
| 56 | if (is_array($messages)) |
---|
| 57 | foreach ($messages as $msg_number) |
---|
| 58 | imap_delete($this->mbox, $msg_number); |
---|
| 59 | |
---|
| 60 | imap_expunge ( $this->mbox ); |
---|
| 61 | |
---|
| 62 | $this->classOAuth->unsetRefreshToken($this->refreshToken); |
---|
| 63 | $this->classOAuth->unsetAccessToken($this->accessToken); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /* |
---|
| 67 | * |
---|
| 68 | * |
---|
| 69 | */ |
---|
| 70 | //(sem mensagens novas) |
---|
| 71 | public function testGet_emptyFolder() |
---|
| 72 | { |
---|
| 73 | |
---|
| 74 | $request = new Request(); |
---|
| 75 | $get = $this->class->get($request); |
---|
| 76 | $body = json_decode($get->body); |
---|
| 77 | $this->assertCount(0, $body); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /* |
---|
| 81 | * |
---|
| 82 | * |
---|
| 83 | */ |
---|
| 84 | //(uma mensagem nova) |
---|
| 85 | public function testGet() |
---|
| 86 | { |
---|
| 87 | $msg = (" MIME-Version: 1.0\r\n" |
---|
| 88 | . "Content-Type: text/plain; charset=ISO-8859-1\r\n" |
---|
| 89 | . "Content-Transfer-Encoding: quoted-printable\r\n" |
---|
| 90 | . "Subject: This is the subject\r\n" |
---|
| 91 | . "From: =?ISO-8859-1?Q?jo=E3o_Silva?= <teste@teste.com.br>\r\n" |
---|
| 92 | . "To: teste@test\r\n" |
---|
| 93 | . "Cc:\r\n" |
---|
| 94 | . "Bcc:\r\n" |
---|
| 95 | . "\r\n\r\n" |
---|
| 96 | . "Corpo\r\n\r\n"); |
---|
| 97 | |
---|
| 98 | imap_append($this->mbox,'{'.$this->cyrusConf['host'].":".$this->cyrusConf['port'].$this->cyrusOPTS.'}INBOX',$msg); |
---|
| 99 | |
---|
| 100 | $request = new Request(); |
---|
| 101 | $get = $this->class->get($request); |
---|
| 102 | $body = json_decode($get->body,true); |
---|
| 103 | $this->assertCount(1, $body); |
---|
| 104 | |
---|
| 105 | $body = $body[0]; |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | $this->assertEquals($body['subject'], 'This is the subject'); |
---|
| 109 | $this->assertEquals($body['from']['mail'], 'teste@teste.com.br'); |
---|
| 110 | $this->assertEquals($body['from']['name'], 'joão Silva'); |
---|
| 111 | $this->assertInternalType('int', $body['date']); |
---|
| 112 | |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | ?> |
---|