Plugin Directory

Changeset 777152


Ignore:
Timestamp:
09/23/2013 08:54:40 AM (13 years ago)
Author:
codebykat
Message:

Better living through science (e.g. TESTS)

Location:
post-by-email/trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • post-by-email/trunk/class-post-by-email.php

    r777049 r777152  
    2525     * @var     string
    2626     */
    27     protected $version = '1.0.3';
     27    protected $version = '1.0.4';
    2828
    2929    /**
     
    245245        update_option( 'post_by_email_options', $options );
    246246
    247         $this->connection = $this->open_mailbox_connection( $options );
    248 
    249         if (! $this->connection ) {
     247        $connection_options = array(
     248            'username' => $options['mailserver_login'],
     249            'password' => $options['mailserver_pass'],
     250            'hostspec' => $options['mailserver_url'],
     251            'port' => $options['mailserver_port'],
     252            'secure' => $options['ssl'] ? 'ssl' : false,
     253        );
     254
     255        $this->connection = $this->open_mailbox_connection( $connection_options );
     256
     257        if ( ! $this->connection ) {
    250258            return;
    251259        }
     
    255263        if ( 0 === sizeof( $uids ) ) {
    256264            $this->save_log_message( __( 'There doesn’t seem to be any new mail.', 'post-by-email' ) );
    257             $this->connection->shutdown();
     265            $this->close_connection();
    258266            return;
    259267        }
     
    445453        $this->mark_as_read( $uids, $options['delete_messages'] );
    446454
    447         $this->connection->shutdown();
     455        $this->close_connection();
    448456    }
    449457
     
    471479     * @return   object
    472480     */
    473     protected function open_mailbox_connection( $options ) {
    474         $connection_options = array(
    475             'username' => $options['mailserver_login'],
    476             'password' => $options['mailserver_pass'],
    477             'hostspec' => $options['mailserver_url'],
    478             'port' => $options['mailserver_port'],
    479             'secure' => $options['ssl'] ? 'ssl' : false,
    480         );
    481 
     481    protected function open_mailbox_connection( $connection_options ) {
    482482        if ( 'POP3' == $options['mailserver_protocol'] ) {
     483            $this->protocol = 'POP3';
    483484            $connection = new Horde_Imap_Client_Socket_Pop3( $connection_options );
    484             $this->protocol = 'POP3';
    485485        } else {  // IMAP
     486            $this->protocol = 'IMAP';
    486487            $connection = new Horde_Imap_Client_Socket( $connection_options );
    487             $this->protocol = 'IMAP';
    488488        }
    489489        $connection->_setInit( 'authmethod', 'USER' );
     
    498498
    499499        return $connection;
     500    }
     501
     502    /**
     503    * Closes the connection to the mailserver.
     504    *
     505    * @since    1.0.4
     506    */
     507    protected function close_connection() {
     508        $this->connection->shutdown();
    500509    }
    501510
  • post-by-email/trunk/post-by-email.php

    r773033 r777152  
    1313 * Plugin URI:  http://codebykat.wordpress.com
    1414 * Description: Create new posts on your WordPress blog by sending email to a specific email address.
    15  * Version:     1.0.3
     15 * Version:     1.0.4
    1616 * Author:      Kat Hagan
    1717 * Author URI:  http://profiles.wordpress.org/codebykat
  • post-by-email/trunk/readme.md

    r777065 r777152  
    127127
    1281281. WordPress version (e.g. 3.6.x).
    129 1. Plugin version (e.g. 1.0.3).
     1291. Plugin version (e.g. 1.0.4).
    1301301. PHP version (e.g. 5.2.x or 5.3.x).
    1311311. Your mailbox settings (URL, protocol, port and whether SSL is enabled; not your login and password).
     
    140140* Added support for user-included gallery shortcode (allows use of WP's gallery options).
    141141* Added warning when checking email before changed settings have been saved.
     142* Added more unit tests.
    142143* Code style fixes as per http://gsoc.trac.wordpress.org/ticket/377
    143144* Fixed https://github.com/codebykat/wp-post-by-email/issues/3
  • post-by-email/trunk/readme.txt

    r777065 r777152  
    44Requires at least: 3.6
    55Tested up to: 3.7
     6Stable tag: trunk
    67License: GPLv2 or later
    78License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    9798== Testing ==
    9899
    99 ### Automated Tests ###
     100= Automated Tests =
    100101
    101102Like WordPress Core, this plugin includes automated unit tests written in PHPUnit.
     
    1061071. From the plugin directory, run `WP_TESTS_DIR=/path/to/WordPress/test/install phpunit`
    107108
    108 ### Manual Testing ###
     109= Manual Testing =
    109110
    1101111. Set up a test email address (services such as Gmail work great for this) and enable IMAP or POP3 access.  (Refer to <a href="http://wordpress.org/plugins/post-by-email/installation/">Installation</a> for detailed instructions.)
     
    1131141. Navigate to the "Activity Log" tab and press the "Check Now" button.  When the page reloads, you should see a new entry in the log file describing the results of the mail check.
    114115
    115 **Caveat** 
    116 There is sometimes a delay between sending an email and having it show up in the mailbox, especially with POP3.  If you're using IMAP, you can connect using a third-party mail client to verify that messages have been received, then mark them as unread so Post By Email will pick them up.
     116**Caveat:** There is sometimes a delay between sending an email and having it show up in the mailbox, especially with POP3.  If you're using IMAP, you can connect using a third-party mail client to verify that messages have been received, then mark them as unread so Post By Email will pick them up.
    117117
    118118
     
    124124
    1251251. WordPress version (e.g. 3.6.x).
    126 1. Plugin version (e.g. 1.0.3).
     1261. Plugin version (e.g. 1.0.4).
    1271271. PHP version (e.g. 5.2.x or 5.3.x).
    1281281. Your mailbox settings (URL, protocol, port and whether SSL is enabled; not your login and password).
     
    137137* Added support for user-included gallery shortcode (allows use of WP's gallery options).
    138138* Added warning when checking email before changed settings have been saved.
     139* Added more unit tests.
    139140* Code style fixes as per http://gsoc.trac.wordpress.org/ticket/377
    140141* Fixed https://github.com/codebykat/wp-post-by-email/issues/3
  • post-by-email/trunk/tests/test-post-by-email.php

    r764618 r777152  
    11<?php
    2 
     2/**
     3 * Post By Email Unit Tests
     4 *
     5 * @package   PostByEmail
     6 * @author    Kat Hagan <kat@codebykat.com>
     7 * @license   GPL-2.0+
     8 * @link      http://codebykat.wordpress.com
     9 * @copyright 2013 Kat Hagan
     10 */
     11
     12/**
     13 * Plugin test class.
     14 *
     15 * @package PostByEmail
     16 * @author  Kat Hagan <kat@codebykat.com>
     17 */
    318class Tests_Post_By_Email_Plugin extends WP_UnitTestCase {
    419
     20    /**
     21     * Instantiation of the plugin.
     22     *
     23     * @since   1.0.4
     24     *
     25     * @var     object
     26     */
    527    protected $plugin;
    628
    7     function setUp() {
     29    /**
     30    * Set up the tests.
     31    *
     32    * @since    1.0.4
     33    */
     34    public function setUp() {
    835        parent::setUp();
    936        $this->plugin = Post_By_Email::get_instance();
    10         $this->pluginAdmin = Post_By_Email_Admin::get_instance();
    11     }
    12 
    13     function test_plugin_activation() {
     37        $options = Post_By_Email::$default_options;
     38        $option['status'] = '';
     39        update_option( 'post_by_email_options', $options );
     40    }
     41
     42    /**
     43    * Set an option in the plugin's options array.
     44    *
     45    * @since    1.0.4
     46    *
     47    * @var      $key    Option name
     48    * @var      $value  Option value
     49    */
     50    protected function set_option( $key, $value ) {
     51        $options = get_option( 'post_by_email_options' );
     52        $options[ $key ] = $value;
     53        update_option( 'post_by_email_options', $options );
     54    }
     55
     56    /**
     57    * Get an option from the plugin's options array.
     58    *
     59    * @since    1.0.4
     60    *
     61    * @var      string    Option name to retrieve
     62    *
     63    * @return   string    Option's value
     64    */
     65    protected function get_option( $key ) {
     66        $options = get_option( 'post_by_email_options' );
     67        return $options[ $key ];
     68    }
     69
     70    /**
     71    * Get the last message logged by the plugin.
     72    *
     73    * @since    1.0.4
     74    *
     75    * @return   string    Last message logged
     76    */
     77    protected function get_last_log_message() {
     78        $log = get_option( 'post_by_email_log' );
     79        if ( $log ) {
     80            $last_entry = array_shift( $log );
     81            return $last_entry['message'];
     82        }
     83        return 'Nothing logged.';
     84    }
     85
     86    /**
     87    * Test plugin activation.
     88    *
     89    * @since    0.9.7
     90    */
     91    public function test_plugin_activation() {
    1492        // with no preexisting options and no global ones, use defaults
    1593        delete_option( 'post_by_email_options' );
     
    1795        $this->plugin->activate( false );
    1896
    19         $options = get_option( 'post_by_email_options' );
    20         $this->assertEquals( 'mail.example.com', $options['mailserver_url'] );
     97        $this->assertEquals( 'mail.example.com', $this->get_option( 'mailserver_url' ) );
    2198
    2299        // copy over the global options if they exist
     
    26103
    27104        $this->assertNotEquals( false, get_option ('post_by_email_options' ) );
    28         $options = get_option( 'post_by_email_options' );
    29         $this->assertEquals( 'testing.example.com', $options['mailserver_url'] );
     105        $this->assertEquals( 'testing.example.com', $this->get_option( 'mailserver_url' ) );
    30106
    31107        // when we have preexisting options, those should take precedence
    32108        update_option( 'mailserver_url', 'another.example.com' );
    33109        $this->plugin->activate( false );
    34         $options = get_option( 'post_by_email_options' );
    35         $this->assertNotEquals( 'another.example.com', $options['mailserver_url'] );
    36     }
    37 
    38     function test_wp_cron_setup() {
     110        $this->assertNotEquals( 'another.example.com', $this->get_option( 'mailserver_url' ) );
     111    }
     112
     113    /**
     114    * Test setup of wp_cron on activate/deactivate.
     115    *
     116    * @since    0.9.8
     117    */
     118    public function test_wp_cron_setup() {
    39119        // plugin activation should schedule an event with wp_cron
    40120        $this->plugin->activate( false );
     
    46126    }
    47127
    48     function test_check_email() {
    49         $this->markTestIncomplete();
    50     }
    51 
    52     function test_save_log_message() {
    53         // testing a protected method: bad idea?
    54         $this->markTestIncomplete();
    55 
    56         // // if the log doesn't exist, it should be created
    57         // $options = get_option( 'post_by_email_options' );
    58         // unset($options['log']);
    59         // update_option( 'post_by_email_options', $options);
    60 
    61         // $this->plugin->save_log_message("Test message");
    62 
    63         // $options = get_option( 'post_by_email_options' );
    64         // $this->assertEquals( 1, sizeof( $options['log'] ) );
    65 
    66         // // if the log exists already, the new message should be added to it
    67         // $this->plugin->save_log_message("Test message two");
    68 
    69         // $options = get_option( 'post_by_email_options' );
    70         // $this->assertEquals( 2, sizeof( $options['log'] ) );
    71     }
     128    /**
     129    * Test that plugin does nothing if options haven't been set.
     130    *
     131    * @since    1.0.4
     132    */
     133    public function test_return_if_options_not_set() {
     134        $this->set_option( 'mailserver_url', 'mail.example.com' );
     135        $this->set_option( 'status', 'unconfigured' );
     136
     137        $stub = $this->getMock( 'Post_By_Email', array( 'open_mailbox_connection', 'get_messages', 'close_connection' ), array(), '', false );
     138
     139        $stub->check_email();
     140
     141        // should immediately return without doing anything
     142        $stub->expects( $this->never() )
     143            ->method( 'open_mailbox_connection' );
     144
     145        $stub->expects( $this->never() )
     146            ->method( 'get_messages' );
     147
     148        $this->assertEquals( 'Nothing logged.', $this->get_last_log_message() );
     149    }
     150
     151    /**
     152    * Test opening a POP3 connection with the right options.
     153    *
     154    * @since    1.0.4
     155    */
     156    public function test_open_POP3_connection() {
     157        $stub = $this->getMock( 'Post_By_Email', array( 'open_mailbox_connection', 'get_messages', 'close_connection' ), array(), '', false );
     158
     159        $this->set_option( 'mailserver_url', 'mail.test.com' );
     160        $this->set_option( 'mailserver_port', '110' );
     161        $this->set_option( 'ssl', false );
     162        $this->set_option( 'mailserver_protocol', 'POP3' );
     163        $this->set_option( 'mailserver_login', 'test@test.com' );
     164        $this->set_option( 'mailserver_pass', 'password' );
     165        $this->set_option( 'status', '' );
     166
     167        $connection_options = array(
     168            'username' => 'test@test.com',
     169            'password' => 'password',
     170            'hostspec' => 'mail.test.com',
     171            'port' => 110,
     172            'secure' => false,
     173        );
     174
     175        $stub->expects( $this->once() )
     176            ->method( 'open_mailbox_connection' )
     177            ->with( $this->equalTo( $connection_options ) );
     178
     179        $stub->check_email();
     180
     181        $this->assertEquals( 'Nothing logged.', $this->get_last_log_message() );
     182    }
     183
     184    /**
     185    * Test checking mailbox with no new messages found.
     186    *
     187    * @since    1.0.4
     188    */
     189    public function test_check_email_no_new_messages() {
     190        $this->set_option( 'status', '' );
     191
     192        $stub = $this->getMock( 'Post_By_Email', array( 'open_mailbox_connection', 'get_messages', 'close_connection' ), array(), '', false );
     193
     194        $stub->expects( $this->once() )
     195            ->method( 'open_mailbox_connection' )
     196            ->will( $this->returnValue( true ) );
     197
     198        $stub->expects( $this->once() )
     199            ->method( 'get_messages' )
     200            ->will( $this->returnValue( array() ) );
     201
     202        $stub->expects( $this->once() )
     203            ->method( 'close_connection' );
     204
     205        $stub->check_email();
     206
     207        $this->assertEquals( "There doesn&#8217;t seem to be any new mail.", $this->get_last_log_message() );
     208    }
     209
     210    /**
     211    * Test checking mailbox and finding a new message.
     212    *
     213    * @since    1.0.4
     214    */
     215    public function test_check_mail() {
     216        $this->set_option( 'status', '' );
     217
     218        $methods_to_stub = array(
     219            'open_mailbox_connection',
     220            'get_messages',
     221            'close_connection',
     222            'get_message_headers',
     223            'get_message_body',
     224            'save_attachments',
     225            'mark_as_read',
     226        );
     227        $stub = $this->getMock( 'Post_By_Email', $methods_to_stub, array(), '', false );
     228
     229        $stub->expects( $this->once() )
     230            ->method( 'open_mailbox_connection' )
     231            ->will( $this->returnValue( true ) );
     232
     233        $message_text = file_get_contents( 'messages/message_with_attachments', true );
     234        $headers = Horde_Mime_Headers::parseHeaders( $message_text );
     235
     236        $stub->expects( $this->once() )
     237            ->method( 'get_messages' )
     238            ->will( $this->returnValue( array( 1 ) ) );
     239
     240        $stub->expects( $this->once() )
     241            ->method( 'get_message_headers' )
     242            ->will( $this->returnValue( $headers ) );
     243
     244        $message = Horde_Mime_Part::parseMessage( $message_text );
     245        $body = $message->getPart('1.1')->toString();
     246
     247        $stub->expects( $this->once() )
     248            ->method( 'get_message_body' )
     249            ->will( $this->returnValue( $body ) );
     250
     251        $stub->expects( $this->once() )
     252            ->method( 'save_attachments' );
     253
     254        $stub->expects( $this->once() )
     255            ->method( 'mark_as_read' );
     256
     257        $stub->expects( $this->once() )
     258            ->method( 'close_connection' );
     259
     260        $stub->check_email();
     261
     262        $this->stringContains( "Found 1 new message.", $this->get_last_log_message() );
     263        $this->assertRegExp( '/Posted(.*?)' . $headers->getValue('Subject') . '/', $this->get_last_log_message() );
     264    }
     265
    72266}
Note: See TracChangeset for help on using the changeset viewer.