
#####
Email
#####


****
NAME
****


Kernel::System::UnitTest::Email - Helper to unit test emails


********
SYNOPSIS
********


Functions to unit test emails


****************
PUBLIC INTERFACE
****************


new()
=====


create an object


.. code-block:: perl

     use Kernel::System::ObjectManager;
     local $Kernel::OM = Kernel::System::ObjectManager->new();
     my $UnitTestEmailObject = $Kernel::OM->Get('Kernel::System::UnitTest::Email');



MailCleanup()
=============


Removes existing emails from Email::Test backend object.


.. code-block:: perl

     my $Success = $UnitTestEmailObject->MailCleanup();


Returns:


.. code-block:: perl

     my $Success = 1;



MailObjectDiscard()
===================


Discards the following objects:
    Kernel::System::Ticket
    Kernel::System::Email::Test
    Kernel::System::Email

and triggers transaction notifications.

Also re-initializes the above objects.


.. code-block:: perl

     my $Success = $UnitTestEmailObject->MailObjectDiscard();


Returns:


.. code-block:: perl

     my $Success = 1;



MailBackendSetup()
==================


Configures Kernel::System::Email::Test as email backend
and disables email address check.


.. code-block:: perl

     my $Success = $UnitTestEmailObject->MailBackendSetup();


Returns:


.. code-block:: perl

     my $Success = 1;



EmailGet()
==========


Fetches emails from email test backend and returns array of hash references containing emails.


.. code-block:: perl

     my @Emails = $UnitTestEmailObject->EmailGet();


Returns:


.. code-block:: perl

     @Emails = (
         {
             Header  => "Email1 Header text...",
             Body    => "Email1 Header text...",
             ToArray => [
                 'email1realrecipient1@test.com',
                 'email1realrecipient2@test.com',
                 'email1realrecipient1@test.com',
             ],
         },
         {
             Header => "Email2 Header text...",
             Body => "Email2 Header text...",
             ToArray => [ 'email2realrecipient1@test.com', ],
         },
         # ...
     );



EmailSentCount()
================


This function counts the number of sent emails.


.. code-block:: perl

     $UnitTestEmailObject->EmailSentCount(
         UnitTestObject => $Self,
         Count          => 3,                               # Expected number of sent emails
         Message        => '3 emails must have been sent.', # Message printed for unit test
     );



EmailValidate()
===============


Checks if the sent emails match the given criteria.

Example:


.. code-block:: perl

     my $Success = $UnitTestEmailObject->EmailValidate(
         UnitTestObject => $Self,
         UnitTestFalse  => 1,                                         # optional, validation should get negated
         Message        => 'Sent emails must contain expected data.', # Message printed for unit test
         Email          => \@Emails,                                  # optional, result of EmailGet() will be used by default
         Header         => qr{To\:\sto\@test.com}xms,                 # Regex or array of regexes that the headers of the sent emails have to match
                                                                      #    example: [ qr{To\:\sto\@test.com}xms, qr{To\:\scc\@test.com}xms, ],
         Body           => qr{Hello [ ] World}xms,                    # Regex or string that the body of the sent emails have to match
         ToArray        => 'email1realrecipient1@test.com',           # Array of strings, string, array of regexes or regex with recipients the sent emails have to match
     );


Returns:


.. code-block:: perl

     my $Success = 1; # or 0 if not found




