[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/core/ -> class-bp-email.php (source)

   1  <?php
   2  /**
   3   * @group core
   4   * @group BP_Email
   5   */
   6  class BP_Tests_Email extends BP_UnitTestCase_Emails {
   7      protected $u1;
   8      protected $u2;
   9  
  10  	public function setUp() {
  11          parent::setUp();
  12  
  13          $this->u1 = self::factory()->user->create( array(
  14              'display_name' => 'Unit Test',
  15              'user_email'   => 'test1@example.com',
  16          ) );
  17  
  18          $this->u2 = self::factory()->user->create( array(
  19              'display_name' => 'Unit Test2',
  20              'user_email'   => 'test2@example.com',
  21          ) );
  22      }
  23  
  24  	public function test_valid_subject() {
  25          $message = 'test';
  26          $email   = new BP_Email( 'activity-at-message' );
  27  
  28          $email->set_subject( $message )->set_tokens( array( 'poster.name' => 'example' ) );
  29          $this->assertSame( $message, $email->get_subject() );
  30      }
  31  
  32  	public function test_valid_html_content() {
  33          $message = '<b>test</b>';
  34          $email   = new BP_Email( 'activity-at-message' );
  35  
  36          $email->set_content_html( $message );
  37          $email->set_content_type( 'html' );
  38  
  39          $this->assertSame( $message, $email->get_content() );
  40      }
  41  
  42  	public function test_valid_plaintext_content() {
  43          $message = 'test';
  44          $email   = new BP_Email( 'activity-at-message' );
  45  
  46          $email->set_content_plaintext( $message );
  47          $email->set_content_type( 'plaintext' );
  48  
  49          $this->assertSame( $message, $email->get_content() );
  50      }
  51  
  52  	public function test_valid_template() {
  53          $message = 'test';
  54          $email   = new BP_Email( 'activity-at-message' );
  55  
  56          $email->set_template( $message );
  57          $this->assertSame( $message, $email->get_template() );
  58      }
  59  
  60  	public function test_tokens() {
  61          $email          = new BP_Email( 'activity-at-message' );
  62          $default_tokens = $email->get_tokens();
  63          $tokens         = array( 'test1' => 'hello', 'test2' => 'world' );
  64  
  65          $email->set_tokens( $tokens );
  66  
  67          $this->assertSame(
  68              array_keys( $tokens + $default_tokens ),
  69              array_keys( $email->get_tokens() )
  70          );
  71  
  72          $this->assertSame(
  73              array_values( $tokens + $default_tokens ),
  74              array_values( $email->get_tokens() )
  75          );
  76      }
  77  
  78  	public function test_headers() {
  79          $email           = new BP_Email( 'activity-at-message' );
  80          $default_headers = $email->get_headers();
  81          $headers         = array( 'custom_header' => 'custom_value' );
  82  
  83          $email->set_headers( $headers );
  84  
  85          $this->assertSame( $headers + $default_headers, $email->get_headers() );
  86      }
  87  
  88  	public function test_validation() {
  89          $email = new BP_Email( 'activity-at-message' );
  90          $email->set_from( 'test1@example.com' )->set_to( 'test2@example.com' )->set_subject( 'testing' );
  91          $email->set_content_html( 'testing' )->set_tokens( array( 'poster.name' => 'example' ) );
  92  
  93          $this->assertTrue( $email->validate() );
  94      }
  95  
  96      public function test_invalid_characters_are_stripped_from_tokens() {
  97          $email          = new BP_Email( 'activity-at-message' );
  98          $default_tokens = $email->get_tokens();
  99  
 100          $email->set_tokens( array( 'te{st}1' => 'hello world' ) );
 101  
 102          $this->assertSame(
 103              array_keys( array( 'test1' => 'hello world' ) + $default_tokens ),
 104              array_keys( $email->get_tokens() )
 105          );
 106      }
 107  
 108  	public function test_token_are_escaped() {
 109          $token = '<blink>';
 110          $email = new BP_Email( 'activity-at-message' );
 111          $email->set_content_html( '{{test}}' )->set_tokens( array( 'test' => $token ) );
 112  
 113          $this->assertSame(
 114              esc_html( $token ),
 115              $email->get_content( 'replace-tokens' )
 116          );
 117      }
 118  
 119  	public function test_token_are_not_escaped() {
 120          $token = '<blink>';
 121          $email = new BP_Email( 'activity-at-message' );
 122          $email->set_content_html( '{{{test}}}' )->set_tokens( array( 'test' => $token ) );
 123  
 124          $this->assertSame(
 125              $token,
 126              $email->get_content( 'replace-tokens' )
 127          );
 128      }
 129  
 130  	public function test_invalid_headers() {
 131          $email           = new BP_Email( 'activity-at-message' );
 132          $default_headers = $email->get_headers();
 133          $headers         = array( 'custom:header' => 'custom:value' );
 134  
 135          $email->set_headers( $headers );
 136  
 137          $this->assertNotSame( $headers + $default_headers, $email->get( 'headers' ) );
 138          $this->assertSame( array( 'customheader' => 'customvalue' ) + $default_headers, $email->get( 'headers' ) );
 139      }
 140  
 141  	public function test_validation_with_missing_required_data() {
 142          $email  = new BP_Email( 'activity-at-message' );
 143          $email->set_from( 'test1@example.com' )->set_to( 'test2@example.com' )->set_subject( 'testing' );  // Content
 144          $email->set_tokens( array( 'poster.name' => 'example' ) );
 145          $result = $email->validate();
 146  
 147          $this->assertTrue( is_wp_error( $result ) );
 148          $this->assertSame( 'missing_parameter', $result->get_error_code() );
 149      }
 150  
 151  	public function test_validation_with_missing_template() {
 152          $email  = new BP_Email( 'activity-at-message' );
 153          $email->set_from( 'test1@example.com' )->set_to( 'test2@example.com' )->set_subject( 'testing' );
 154          $email->set_content_html( 'testing' )->set_tokens( array( 'poster.name' => 'example' ) )->set_template( '' );
 155          $result = $email->validate();
 156  
 157          // Template has a default value, but it can't be blank.
 158          $this->assertTrue( is_wp_error( $result ) );
 159          $this->assertSame( 'missing_parameter', $result->get_error_code() );
 160      }
 161  
 162      public function test_invalid_tags_should_be_removed_from_html_content() {
 163          $message = '<b>hello world</b><iframe src="https://example.com"></iframe><b>hello world</b>';
 164          $email   = new BP_Email( 'activity-at-message' );
 165  
 166          $email->set_content_html( $message );
 167          $email->set_content_type( 'html' );
 168  
 169          $this->assertSame( '<b>hello world</b><b>hello world</b>', $email->get_content() );
 170      }
 171  
 172      public function test_multiple_recipients_are_supported_by_address() {
 173          $email1 = 'test1@example.com';
 174          $email2 = 'test2@example.com';
 175          $email  = new BP_Email( 'activity-at-message' );
 176  
 177          $email->set_to( array( $email1, $email2 ) );
 178          $addresses = $email->get_to();
 179  
 180          $this->assertCount( 2, $addresses );
 181          $this->assertSame( $email1, $addresses[0]->get_address() );
 182          $this->assertSame( $email2, $addresses[1]->get_address() );
 183      }
 184  
 185      public function test_multiple_recipients_are_supported_by_wp_user_object() {
 186          $user1 = get_user_by( 'id', $this->u1 );
 187          $user2 = get_user_by( 'id', $this->u2 );
 188          $email = new BP_Email( 'activity-at-message' );
 189  
 190          $email->set_to( array( $user1, $user2 ) );
 191          $addresses = $email->get_to();
 192  
 193          $this->assertCount( 2, $addresses );
 194          $this->assertSame( $user1->user_email, $addresses[0]->get_address() );
 195          $this->assertSame( $user2->user_email, $addresses[1]->get_address() );
 196      }
 197  
 198      public function test_multiple_recipients_are_supported_by_wp_user_id() {
 199          $user1 = get_user_by( 'id', $this->u1 );
 200          $user2 = get_user_by( 'id', $this->u2 );
 201          $email = new BP_Email( 'activity-at-message' );
 202          $email->set_to( array( $this->u1, $this->u2 ) );
 203          $addresses = $email->get_to();
 204  
 205          $this->assertCount( 2, $addresses );
 206          $this->assertSame( $user1->user_email, $addresses[0]->get_address() );
 207          $this->assertSame( $user2->user_email, $addresses[1]->get_address() );
 208      }
 209  
 210  	public function test_multiple_recipients_are_supported() {
 211          $user1 = get_user_by( 'id', $this->u1 );
 212          $user2 = get_user_by( 'id', $this->u2 );
 213          $user3 = 'test3@example.com';
 214          $email = new BP_Email( 'activity-at-message' );
 215  
 216          $email->set_to( array( $user1, $this->u2, $user3 ) );
 217          $addresses = $email->get_to();
 218  
 219          $this->assertCount( 3, $addresses );
 220          $this->assertSame( $user1->user_email, $addresses[0]->get_address() );
 221          $this->assertSame( $user2->user_email, $addresses[1]->get_address() );
 222          $this->assertSame( $user3,             $addresses[2]->get_address() );
 223      }
 224  
 225      public function test_replacing_existing_recipients_with_new_recipients() {
 226          $email              = new BP_Email( 'activity-at-message' );
 227          $original_recipient = 'test1@example.com';
 228          $new_recipient      = 'test2@example.com';
 229  
 230          $email->set_to( $original_recipient );
 231          $addresses = $email->get_to();
 232          $this->assertSame( $original_recipient, $addresses[0]->get_address() );
 233  
 234          $email->set_to( $new_recipient );
 235          $addresses = $email->get_to();
 236          $this->assertSame( $new_recipient, $addresses[0]->get_address() );
 237      }
 238  
 239      public function test_appending_new_recipients_to_existing_recipients() {
 240          $email              = new BP_Email( 'activity-at-message' );
 241          $original_recipient = 'test1@example.com';
 242          $new_recipient      = 'test2@example.com';
 243  
 244          $email->set_to( $original_recipient );
 245          $addresses = $email->get_to();
 246          $this->assertSame( $original_recipient, $addresses[0]->get_address() );
 247  
 248          $email->set_to( $new_recipient, '', 'add' );
 249          $addresses = $email->get_to();
 250          $this->assertSame( $original_recipient, $addresses[0]->get_address() );
 251          $this->assertSame( $new_recipient, $addresses[1]->get_address() );
 252      }
 253  
 254  	public function test_sending_email() {
 255          require_once( BP_PLUGIN_DIR . '/bp-core/admin/bp-core-admin-schema.php' );
 256          bp_core_install_emails();
 257  
 258          $user1  = get_user_by( 'id', $this->u1 );
 259          $result = bp_send_email( 'activity-comment', $this->u1, array(
 260              'tokens' => array(
 261                  'comment.id'                => 123,
 262                  'commenter.id'              => $this->u2,
 263                  'usermessage'               => 'hello world',
 264                  'original_activity.user_id' => $this->u1,
 265                  'poster.name'               => 'name',
 266                  'thread.url'                => 'http://example.com',
 267                  'unsubscribe'               => 'http://example.com',
 268              ),
 269          ) );
 270  
 271          $this->assertTrue( $result );
 272      }
 273  
 274      public function test_html_entities_are_decoded_in_email_subject() {
 275          // Emulate custom post title for an email post type.
 276          $subject = "It's pretty <new & magical.";
 277  
 278          $email = new BP_Email( 'activity-at-message' );
 279          $email->set_subject( $subject )->set_tokens( array( 'poster.name' => 'blah' ) );
 280  
 281          // Subject always has to have tokens replaced before sending.
 282          $this->assertSame( $subject, $email->get_subject( 'replace-tokens' ) );
 283      }
 284  
 285      public function test_html_entities_are_decoded_in_email_recipient_names() {
 286          // Raw display name.
 287          $name = "Test o'Toole";
 288  
 289          // Emulate rendered {poster.name} token.
 290          $token = apply_filters( 'bp_core_get_user_displayname', $name );
 291  
 292          $email = new BP_Email( 'activity-at-message' );
 293          $email->set_subject( '{{poster.name}}' )->set_tokens( array( 'poster.name' => $token ) );
 294  
 295          // Subject always has to have tokens replaced before sending.
 296          $this->assertSame( $name, $email->get_subject( 'replace-tokens' ) );
 297      }
 298  
 299  }


Generated: Wed Sep 18 01:01:38 2019 Cross-referenced by PHPXref 0.7.1