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