WebformUnitTestCase.test 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Webform module unit tests.
  4. */
  5. class WebformUnitTestCase extends DrupalUnitTestCase {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => t('Webform unit tests'),
  12. 'description' => t('Unit tests for Webform functions.'),
  13. 'group' => t('Webform'),
  14. );
  15. }
  16. /**
  17. * The tests.
  18. */
  19. public function test() {
  20. require_once __DIR__ . '/../webform.module';
  21. $test = webform_format_email_address('test@example.com', 'John Smith');
  22. $sample = '"John Smith" <test@example.com>';
  23. $this->assertIdentical($test, $sample, 'webform_format_email_address() returns string for single name and email address.');
  24. $test = webform_format_email_address('default', 'default');
  25. $sample = '"' . webform_variable_get('webform_default_from_name') . '" <' . webform_variable_get('webform_default_from_address') . '>';
  26. $this->assertIdentical($test, $sample, 'webform_format_email_address() handles defaults.');
  27. $test = webform_format_email_address('test@example.com', NULL);
  28. $sample = 'test@example.com';
  29. $this->assertIdentical($test, $sample, 'webform_format_email_address() handles NULL name.');
  30. $test = webform_format_email_address('test@example.com', 'John Smith', NULL, NULL, TRUE, FALSE);
  31. $sample = ['"John Smith" <test@example.com>'];
  32. $this->assertIdentical($test, $sample, 'webform_format_email_address() returns array for single name and email address.');
  33. $test = webform_format_email_address(['test1@example.com', 'test2@example.com'], 'John Smith');
  34. $sample = '"John Smith" <test1@example.com>';
  35. $this->assertIdentical($test, $sample, 'webform_format_email_address() returns single string for multiple email addresses by default.');
  36. $test = webform_format_email_address(['test1@example.com', 'test2@example.com'], ['John One', 'John Two'], NULL, NULL, TRUE, FALSE);
  37. $sample = ['"John One" <test1@example.com>', '"John Two" <test2@example.com>'];
  38. $this->assertIdentical($test, $sample, 'webform_format_email_address() returns array for multiple email addresses when $single is FALSE.');
  39. $test = webform_format_email_address(['test1@example.com', 'test2@example.com'], 'John One', NULL, NULL, TRUE, FALSE);
  40. $sample = ['"John One" <test1@example.com>', '"John One" <test2@example.com>'];
  41. $this->assertIdentical($test, $sample, 'webform_format_email_address() repeats first name when more emails than names provided.');
  42. $test = webform_format_email_address('test1@example.com, test2@example.com', 'John One', NULL, NULL, TRUE, FALSE);
  43. $sample = ['"John One" <test1@example.com>', '"John One" <test2@example.com>'];
  44. $this->assertIdentical($test, $sample, 'webform_format_email_address() accepts multiple emails as comma-separated string.');
  45. $node = (object) [
  46. 'webform' => [
  47. 'components' => [
  48. 1 => ['name' => 'Email component', 'type' => 'textfield'],
  49. 2 => ['name' => 'Name component', 'type' => 'textfield'],
  50. ],
  51. ],
  52. ];
  53. $test = webform_format_email_address(1, 2, $node);
  54. $sample = '"Value of Name component" <Value of "Email component">';
  55. $this->assertIdentical($test, $sample, 'webform_format_email_address() takes name and email from component names.');
  56. $submission = (object) [
  57. 'data' => [
  58. 1 => ['test@example.com'],
  59. 2 => ['John Smith'],
  60. ],
  61. ];
  62. $test = webform_format_email_address(1, 2, $node, $submission);
  63. $sample = '"John Smith" <test@example.com>';
  64. $this->assertIdentical($test, $sample, 'webform_format_email_address() takes name and email from submission values.');
  65. }
  66. }