/**
  * The purpose of this test is to ensure that generation of default templates works as expected.
  *
  * @since 4.5.0
  */
 public function test_generate_default_message_templates()
 {
     /**
      * Testing default messengers setup on activation (or introduction on migration)
      */
     //first let's make sure all message templates got setup on new install as they should be.
     EE_Registry::instance()->load_helper('MSG_Template');
     EE_Registry::instance()->load_helper('Activation');
     $installed_messengers = EEH_MSG_Template::get_installed_message_objects();
     $should_be_installed = array();
     foreach ($installed_messengers['messengers'] as $msgr) {
         $this->assertInstanceOf('EE_messenger', $msgr);
         if ($msgr->activate_on_install) {
             $should_be_installed[] = $msgr->name;
         }
     }
     $active_messengers = EEH_MSG_Template::get_active_messengers_in_db();
     //loop through $should_be_installed and verify that those that should be active ARE active.
     foreach ($should_be_installed as $msgr_name) {
         $this->assertTrue(isset($active_messengers[$msgr_name]), sprintf('The messenger %s should be active on fresh install, but it is not.', $msgr_name));
     }
     //now verify that the code doesn't run new message template generation etc.
     $this->assertFalse(EEH_Activation::generate_default_message_templates());
     //now we simulate someone who's deactivated a messenger and we simulate a migration that triggers generating default message templates again.  The html messenger should STICK and NOT be activated.
     unset($active_messengers['html']);
     EEH_MSG_Template::update_active_messengers_in_db($active_messengers);
     $activated_response = EEH_Activation::generate_default_message_templates();
     //verify we got a response (html should generate templates)
     $this->assertFalse($activated_response);
     //doublecheck we still don't html in the active messengers array
     $active_messengers = EEH_MSG_Template::get_active_messengers_in_db();
     $this->assertFalse(isset($active_messengers['html']));
 }
 /**
  * This simply validates active messengers and message types to ensure they actually match installed messengers and message types.  If there's a mismatch then we deactivate the messenger/message type and ensure all related db rows are set inactive.
  *
  * @since 4.3.1
  *
  * @return void
  */
 public static function validate_messages_system()
 {
     //include our helper
     EE_Registry::instance()->load_helper('MSG_Template');
     //get active and installed  messengers/message types.
     $active_messengers = EEH_MSG_Template::get_active_messengers_in_db();
     $installed = EEH_MSG_Template::get_installed_message_objects();
     $installed_messengers = $installed_mts = array();
     //set up the arrays so they can be handled easier.
     foreach ($installed['messengers'] as $im) {
         if ($im instanceof EE_messenger) {
             $installed_messengers[$im->name] = $im;
         }
     }
     foreach ($installed['message_types'] as $imt) {
         if ($imt instanceof EE_message_type) {
             $installed_mts[$imt->name] = $imt;
         }
     }
     //now let's loop through the active array and validate
     foreach ($active_messengers as $messenger => $active_details) {
         //first let's see if this messenger is installed.
         if (!isset($installed_messengers[$messenger])) {
             //not set so let's just remove from actives and make sure templates are inactive.
             unset($active_messengers[$messenger]);
             EEH_MSG_Template::update_to_inactive($messenger);
             continue;
         }
         //messenger is active, so let's just make sure that any active message types not installed are deactivated.
         $mts = !empty($active_details['settings'][$messenger . '-message_types']) ? $active_details['settings'][$messenger . '-message_types'] : array();
         foreach ($mts as $mt_name => $mt) {
             if (!isset($installed_mts[$mt_name])) {
                 unset($active_messengers[$messenger]['settings'][$messenger . '-message_types'][$mt_name]);
                 EEH_MSG_Template::update_to_inactive($messenger, $mt_name);
             }
         }
     }
     //all done! let's update the active_messengers.
     EEH_MSG_Template::update_active_messengers_in_db($active_messengers);
     do_action('AHEE__EEH_Activation__validate_messages_system');
     return;
 }