You are here

class PHPMailerUnitTest in PHPMailer 8.3

Validates e-mail address extraction.

This needs more work, so that it actually works with PHPUnit. It maybe that due to it only testing a single function in the .module file and not a method in a class, it won't work at all.

@group phpmailer

Hierarchy

Expanded class hierarchy of PHPMailerUnitTest

File

tests/src/Unit/PHPMailerUnitTest.php, line 16

Namespace

Drupal\Tests\phpmailer\Unit
View source
class PHPMailerUnitTest extends UnitTestCase {

  /**
   * Tests e-mail address extraction using phpmailer_parse_address().
   */
  function testAddressParser() {

    // Set up various test addresses according to RFC 5322.
    $this->addresses = [
      // addr-spec.
      [
        'mail' => 'user-1@domain.tld',
        'name' => '',
      ],
      // Invalid but supported angle-addr without preceding display-name.
      '<user-2@domain.tld>' => [
        'mail' => 'user-2@domain.tld',
        'name' => '',
      ],
      // Unquoted atom name-addr.
      'John Doe <user-3@domain.tld>' => [
        'mail' => 'user-3@domain.tld',
        'name' => 'John Doe',
      ],
      // Quoted atom name-addr.
      '"John Doe" <user-4@domain.tld>' => [
        'mail' => 'user-4@domain.tld',
        'name' => 'John Doe',
      ],
      // name-addr with a quoted-string in display-name.
      [
        'mail' => 'user-5@domain.tld',
        'name' => 'John "The Dude" Doe',
      ],
      // name-addr with a quoted-string and comma in display-name.
      [
        'mail' => 'user-6@domain.tld',
        'name' => 'John "The Dude" Doe (Foo, Bar)',
      ],
      // name-addr containing non-ASCII chars in display-name.
      [
        'mail' => 'user-7@domain.tld',
        'name' => 'Jöhn "The Düde" Döe',
      ],
    ];
    $all = [];

    // Validate each address format is correctly parsed.
    foreach ($this->addresses as $test => $address) {
      if (is_numeric($test)) {
        if ($address['name'] != '') {

          // Create a valid encoded email address.
          $test = '"' . addslashes(Unicode::mimeHeaderEncode($address['name'])) . '" <' . $address['mail'] . '>';
        }
        else {
          $test = $address['mail'];
        }
      }
      $result = phpmailer_parse_address($test);
      $this
        ->assertEqual($result[0], $address, t('Successfully extracted %email, %name from %address.', [
        '%email' => $result[0]['mail'],
        '%name' => $result[0]['name'] ? $result[0]['name'] : '(blank)',
        '%address' => $test,
      ]), 'PHPMailer');
      $all[] = $test;
    }

    // One final test with all addresses concatenated.
    $result = phpmailer_parse_address(implode(', ', $all));
    $expected_result = array_values($this->addresses);
    $this
      ->assertEqual($result, $expected_result, t('All concatenated e-mail addresses could be extracted.'), 'PHPMailer');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PHPMailerUnitTest::testAddressParser function Tests e-mail address extraction using phpmailer_parse_address().
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340