[BUGFIX] Faulty check for missing SMTP port 18/23518/2
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Sat, 31 Aug 2013 22:40:23 +0000 (00:40 +0200)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Mon, 11 Nov 2013 17:03:41 +0000 (18:03 +0100)
Check also for null port to avoid a fatal error.

Resolves: #31998
Releases: 6.2, 6.1, 6.0, 4.7, 4.5
Change-Id: Ibf45b3c0783a70e5afba33f90d1d8e05f76834cf
Reviewed-on: https://review.typo3.org/23518
Reviewed-by: Markus Klein
Reviewed-by: Stefan Neufeind
Tested-by: Stefan Neufeind
t3lib/mail/class.t3lib_mail_mailer.php
tests/t3lib/mail/class.t3lib_mail_mailerTest.php [new file with mode: 0644]

index 3f37b87..23d823f 100644 (file)
@@ -96,7 +96,7 @@ class t3lib_mail_Mailer extends Swift_Mailer {
                                                1291068606
                                        );
                                }
-                               if ($port === '') {
+                               if ($port === NULL || $port === '') {
                                        $port = '25';
                                }
                                $useEncryption = ($mailSettings['transport_smtp_encrypt'] ? $mailSettings['transport_smtp_encrypt'] : NULL);
@@ -151,4 +151,4 @@ if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLA
        include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_mail_mailer.php']);
 }
 
-?>
\ No newline at end of file
+?>
diff --git a/tests/t3lib/mail/class.t3lib_mail_mailerTest.php b/tests/t3lib/mail/class.t3lib_mail_mailerTest.php
new file mode 100644 (file)
index 0000000..df73138
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+/***************************************************************
+ * Copyright notice
+ *
+ * (c) 2011-2013 Ernesto Baschny (ernst@cron-it.de)
+ * All rights reserved
+ *
+ * This script is part of the TYPO3 project. The TYPO3 project is
+ * free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * The GNU General Public License can be found at
+ * http://www.gnu.org/copyleft/gpl.html.
+ *
+ * This script is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+/**
+ * Testcase for the t3lib_mail_Mailer class.
+ *
+ * @package TYPO3
+ * @subpackage t3lib
+ *
+ * @author Helmut Hummel <helmut.hummel@typo3.org>
+ */
+class t3lib_mail_MailerTest extends tx_phpunit_testcase {
+
+       /**
+        * @var t3lib_mail_Mailer
+        */
+       protected $fixture;
+
+       /**
+        * @var array
+        */
+       protected $globalMailSettings;
+
+
+       public function setUp() {
+               $this->globalMailSettings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
+               $this->fixture = $this->getMock('t3lib_mail_Mailer', array('noMethodMocked'), array(), '', FALSE);
+       }
+
+       public function tearDown() {
+               unset($this->fixture);
+               $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $this->globalMailSettings;
+       }
+
+       //////////////////////////
+       // Tests concerning TYPO3\CMS\Core\Mail\Mailer
+       //////////////////////////
+       /**
+        * Data provider for wrongConfigigurationThrowsException
+        *
+        * @return array Data sets
+        */
+       static public function wrongConfigigurationProvider() {
+               return array(
+                       'smtp but no host' => array(array('transport' => 'smtp')),
+                       'mbox but no file' => array(array('transport' => 'mbox')),
+                       'no instance of Swift_Transport' => array(array('transport' => 'TYPO3\\CMS\\Core\\Messaging\\ErrorpageMessage'))
+               );
+       }
+
+       /**
+        * @test
+        * @param $settings
+        * @dataProvider wrongConfigigurationProvider
+        * @expectedException t3lib_exception
+        */
+       public function wrongConfigigurationThrowsException($settings) {
+               $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $settings;
+               $this->fixture->__construct();
+       }
+
+       /**
+        * @test
+        */
+       public function providingCorrectClassnameDoesNotThrowException() {
+               if (!class_exists('t3lib_mail_SwiftMailerFakeTransport')) {
+                               // Create fake custom transport class
+                       eval('class t3lib_mail_SwiftMailerFakeTransport extends t3lib_mail_MboxTransport {
+                               public function __construct($settings) {}
+                       }');
+               }
+               $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 't3lib_mail_SwiftMailerFakeTransport');
+               $this->fixture->__construct();
+       }
+
+       /**
+        * @test
+        */
+       public function noPortSettingSetsPortTo25() {
+               $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 'smtp', 'transport_smtp_server' => 'localhost');
+               $this->fixture->__construct();
+               $port = $this->fixture->getTransport()->getPort();
+               $this->assertEquals(25, $port);
+       }
+
+       /**
+        * @test
+        */
+       public function emptyPortSettingSetsPortTo25() {
+               $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 'smtp', 'transport_smtp_server' => 'localhost:');
+               $this->fixture->__construct();
+               $port = $this->fixture->getTransport()->getPort();
+               $this->assertEquals(25, $port);
+       }
+
+       /**
+        * @test
+        */
+       public function givenPortSettingIsRespected() {
+               $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 'smtp', 'transport_smtp_server' => 'localhost:12345');
+               $this->fixture->__construct();
+               $port = $this->fixture->getTransport()->getPort();
+               $this->assertEquals(12345, $port);
+       }
+}
+
+?>
\ No newline at end of file