| 1 | <?php |
| 2 | /** |
| 3 | * Tests for the send_debug_email method. |
| 4 | * |
| 5 | * @group upgrader |
| 6 | */ |
| 7 | class Tests_Upgrader_SendDebugEmail extends WP_UnitTestCase { |
| 8 | |
| 9 | |
| 10 | /** |
| 11 | * Lets be sure the mailer is reset before every test. |
| 12 | */ |
| 13 | function setUp() { |
| 14 | parent::setUp(); |
| 15 | unset( $GLOBALS['phpmailer']->mock_sent ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Lets be sure the mailer is reset after every test. |
| 20 | */ |
| 21 | function tearDown() { |
| 22 | parent::tearDown(); |
| 23 | unset( $GLOBALS['phpmailer']->mock_sent ); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * Helper to return some update results |
| 29 | * @return array |
| 30 | */ |
| 31 | protected function update_results() { |
| 32 | return array( |
| 33 | 'core' => array( (object) array( 'result' => true, 'name' => 'Unit Test : Core', 'messages' => array( 'Success' ) ) ), |
| 34 | 'plugin' => array( (object) array( 'result' => true, 'name' => 'Unit Test : Plugin', 'messages' => array( 'Success' ) ) ), |
| 35 | 'theme' => array( (object) array( 'result' => true, 'name' => 'Unit Test : Theme', 'messages' => array( 'Success' ) ) ), |
| 36 | 'translation' => array( (object) array( 'result' => true, 'name' => 'Unit Test : Translation', 'messages' => array( 'Success' ) ) ), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Helper to create the updater instance |
| 42 | */ |
| 43 | protected function setup_and_run_send_debug_email() { |
| 44 | |
| 45 | $upgrader = new Mock_Automatic_Updater(); |
| 46 | $upgrader->trigger_send_debug_email( $this->update_results() ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Baseline test to ensure that an email is sent that contains info for the 4 areas. |
| 51 | * @ticket 33932 |
| 52 | */ |
| 53 | function test_send_debug_email_sends_emails() { |
| 54 | |
| 55 | $this->setup_and_run_send_debug_email(); |
| 56 | |
| 57 | // Retrieve the mailer details. |
| 58 | $mailer = tests_retrieve_phpmailer_instance(); |
| 59 | $body = $mailer->get_sent()->body; |
| 60 | $core = strpos( $body, 'Core' ); |
| 61 | $plugin = strpos( $body, 'Plugin' ); |
| 62 | $theme = strpos( $body, 'Theme' ); |
| 63 | $translation = strpos( $body, 'Translation' ); |
| 64 | |
| 65 | // Assertions. |
| 66 | $this->assertNotSame( false, $core, '"Core" was not found in the body string' ); |
| 67 | $this->assertNotSame( false, $plugin, '"Plugin" was not found in the body string' ); |
| 68 | $this->assertNotSame( false, $theme, '"Theme" was not found in the body string' ); |
| 69 | $this->assertNotSame( false, $translation, '"Translation" was not found in the body string' ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Testing the send_update_notification_email filter |
| 74 | * |
| 75 | * @ticket 33932 |
| 76 | */ |
| 77 | function test_send_update_notification_email_filter_no_emails() { |
| 78 | |
| 79 | /** |
| 80 | * Disable all notifications. |
| 81 | */ |
| 82 | add_filter( 'send_update_notification_email', '__return_false' ); |
| 83 | |
| 84 | $this->setup_and_run_send_debug_email(); |
| 85 | |
| 86 | // Retrieve the mailer details. |
| 87 | $mailer = tests_retrieve_phpmailer_instance(); |
| 88 | $body = $mailer->get_sent()->body; |
| 89 | $plugin = strpos( $body, 'Plugin' ); |
| 90 | $theme = strpos( $body, 'Theme' ); |
| 91 | $translation = strpos( $body, 'Translation' ); |
| 92 | |
| 93 | remove_filter( 'send_update_notification_email', '__return_false' ); |
| 94 | |
| 95 | |
| 96 | // Assertions. |
| 97 | $this->assertSame( false, $plugin, '"Plugin" was found in the body string' ); |
| 98 | $this->assertSame( false, $theme, '"Theme" was found in the body string' ); |
| 99 | $this->assertSame( false, $translation, '"Translation" was found in the body string' ); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * Testing the send_update_notification_email filter |
| 105 | * |
| 106 | * @dataProvider data_send_update_notification_email_filter_remove_single_type |
| 107 | * |
| 108 | * @ticket 33932 |
| 109 | */ |
| 110 | function test_send_update_notification_email_filter_remove_single_type( $type ) { |
| 111 | |
| 112 | /** |
| 113 | * Disable all notifications. |
| 114 | */ |
| 115 | add_filter( 'send_update_notification_email', array( $this, "remove_{$type}_from_notifications" ), 10, 2 ); |
| 116 | |
| 117 | $this->setup_and_run_send_debug_email(); |
| 118 | |
| 119 | // Retrieve the mailer details. |
| 120 | $mailer = tests_retrieve_phpmailer_instance(); |
| 121 | $body = $mailer->get_sent()->body; |
| 122 | $$type = strpos( $body, ucfirst( $type ) ); |
| 123 | |
| 124 | remove_filter( 'send_update_notification_email', array( $this, "remove_{$type}_from_notifications" ), 10, 2 ); |
| 125 | |
| 126 | |
| 127 | // Assertions. |
| 128 | $this->assertSame( false, $$type, '"'.ucfirst( $type ).'" was found in the body string' ); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Data Provider. |
| 134 | * |
| 135 | * Passes the type of update meant to be suppressed. |
| 136 | * |
| 137 | * @since 4.6.0 |
| 138 | * |
| 139 | * @return array { |
| 140 | * @type array { |
| 141 | * @type string The machine name of the type of update. |
| 142 | * } |
| 143 | * } |
| 144 | */ |
| 145 | function data_send_update_notification_email_filter_remove_single_type() { |
| 146 | return array( |
| 147 | array( 'plugin' ), |
| 148 | array( 'theme' ), |
| 149 | array( 'translation' ), |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * Callback for the send_update_notification_email tests. |
| 156 | * |
| 157 | * @param bool $will_send Will the email be sent. |
| 158 | * @param string $type The type of update. |
| 159 | * |
| 160 | * @return bool |
| 161 | */ |
| 162 | function remove_plugin_from_notifications( $will_send, $type ) { |
| 163 | return ( 'plugin' === $type ) ? false : true; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Callback for the send_update_notification_email tests. |
| 168 | * |
| 169 | * @param bool $will_send Will the email be sent. |
| 170 | * @param string $type The type of update. |
| 171 | * |
| 172 | * @return bool |
| 173 | */ |
| 174 | function remove_theme_from_notifications( $will_send, $type ) { |
| 175 | return ( 'theme' === $type ) ? false : true; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Callback for the send_update_notification_email tests. |
| 180 | * |
| 181 | * @param bool $will_send Will the email be sent. |
| 182 | * @param string $type The type of update. |
| 183 | * |
| 184 | * @return bool |
| 185 | */ |
| 186 | function remove_translation_from_notifications( $will_send, $type ) { |
| 187 | return ( 'translation' === $type ) ? false : true; |
| 188 | } |
| 189 | } |