浏览代码

BaseEmailConfig

euromark 11 年之前
父节点
当前提交
3ce2f89f68

+ 2 - 0
.travis.yml

@@ -28,6 +28,8 @@ before_script:
   - echo "Configure::write('Security.cipherSeed', '16659201697453542496749683615');" >> Config/bootstrap.php
   - echo "<?php App::uses('MyModel', 'Tools.Model'); class AppModel extends MyModel {}" > Model/AppModel.php
   - echo "<?php App::uses('MyController', 'Tools.Controller'); class AppController extends MyController {}" > Controller/AppController.php
+  - echo "<?php App::uses('BaseEmailConfig', 'Tools.Config'); class EmailConfig extends BaseEmailConfig {}" > Config/email.php
+  - echo "Configure::write('Config.adminEmail', 'example@example.org');" >> Config/bootstrap.php
   - cd ..
   - cp ../tools/phpunit.xml app/
 

+ 2 - 33
Controller/Component/AuthExtComponent.php

@@ -10,9 +10,6 @@ if (!defined('CLASS_USER')) {
 App::uses('AuthComponent', 'Controller/Component');
 
 /**
- * Important:
- * index the ACO on alias, index the Aro on model+id
- *
  * Extends AuthComponent with the following addons:
  * - allows multiple roles per user
  * - auto-raises login counter and sets last_login date
@@ -39,15 +36,12 @@ class AuthExtComponent extends AuthComponent {
 
 	public $loginError = null;
 
-	public $settings = array(
+	protected $_defaults = array(
 		'multi' => null, # null=auto - yes/no multiple roles (HABTM table between users and roles)
 		'parentModelAlias' => USER_ROLE_KEY,
 		'userModel' => CLASS_USER //TODO: allow plugin syntax
 	);
 
-	// field name in DB , if none is specified there will be no floodProtection
-	public $floodProtection = null;
-
 	/**
 	 * Merge in Configure::read('Auth') settings
 	 *
@@ -55,7 +49,7 @@ class AuthExtComponent extends AuthComponent {
 	 * @param mixed $settings
 	 */
 	public function __construct(ComponentCollection $Collection, $settings = array()) {
-		$settings = array_merge($this->settings, (array)Configure::read('Auth'), (array)$settings);
+		$settings = array_merge($this->_defaults, (array)Configure::read('Auth'), $settings);
 
 		parent::__construct($Collection, $settings);
 	}
@@ -188,18 +182,9 @@ class AuthExtComponent extends AuthComponent {
 				$userArray[$parentModelAlias] = array(); # default: no roles!
 				$roles = $this->{$withModel}->find('list', array('fields' => array($withModel . '.role_id'), 'conditions' => array($withModel . '.user_id' => $userArray['id'])));
 				if (!empty($roles)) {
-					//$primaryRole = $this->user($this->fieldKey);
-					// retrieve associated role that are not the primary one
-
-					// MAYBE USEFUL FOR GUEST!!!
-					//$roles = set::extract('/'.$with.'['.$this->fieldKey.'!='.$primaryRole.']/'.$this->fieldKey, $roles);
-
 					// add the suplemental roles id under the Auth session key
 					$userArray[$parentModelAlias] = $roles;
-					//pr($completeAuth);
 				}
-			} else {
-				//$userArray[$parentModelAlias][] = $userArray['role_id'];
 			}
 		}
 
@@ -264,20 +249,4 @@ class AuthExtComponent extends AuthComponent {
 		return ClassRegistry::init($model);
 	}
 
-	/**
-	 * @deprecated
-	 * @return boolean Success
-	 */
-	public function verifyUser($id, $pwd) {
-		trigger_error('deprecated - use Authenticate class');
-		$options = array(
-			'conditions' => array('id' => $id, 'password' => $this->password($pwd)),
-		);
-		return $this->getModel()->find('first', $options);
-
-		$this->constructAuthenticate();
-		$this->request->data['User']['password'] = $pwd;
-		return $this->identify($this->request, $this->response);
-	}
-
 }

+ 0 - 66
Lib/Bootstrap/DebugTab.php

@@ -1,66 +0,0 @@
-<?php
-
-/**
- * Debug entries to a static class
- * Do not use App::uses() to include this file as it also needs a function included
- * (see below). Use App::import('Lib', 'Tools.Bootstrap/DebugTab');
- */
-class DebugTab {
-
-	public static $content = array();
-
-	public static $groups = array();
-
-	/**
-	 * @return boolean Success
-	 */
-	public static function debug($var = false, $display = false, $key = null) {
-		if (is_string($display)) {
-			$key = $display;
-			$display = true;
-		}
-		if (Configure::read('debug') > 0) {
-			$calledFrom = debug_backtrace();
-			if (is_string($key)) {
-				if (!isset(DebugTab::$groups[$key])) {
-					DebugTab::$groups[$key] = array();
-				}
-				DebugTab::$groups[$key][] = array(
-					'debug' => print_r($var, true),
-					'file' => substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1),
-					'line' => $calledFrom[0]['line'],
-					'display' => $display
-				);
-			} else {
-				DebugTab::$content[] = array(
-					'debug' => print_r($var, true),
-					'file' => substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1),
-					'line' => $calledFrom[0]['line'],
-					'display' => $display
-				);
-			}
-		}
-		return true;
-	}
-
-	/**
-	 * Display debugged information
-	 *
-	 * @return string HTML
-	 */
-	public static function get() {
-		return '<pre class="debug-tab">' .
-			print_r(DebugTab::$groups, true) .
-			print_r(DebugTab::$content, true) .
-			'</pre>';
-	}
-}
-
-/**
- * Public, quick access function for class
- *
- * @return boolean Success
- */
-function debugTab($var = false, $display = false, $key = null) {
-	return DebugTab::debug($var, $display, $key);
-}

+ 0 - 13
Lib/Bootstrap/MyBootstrap.php

@@ -1,13 +0,0 @@
-<?php
-/**
- * Basic bootstrap stuff
- *
- * Note: Do not use App::uses() to include this file.
- * Use App::import('Lib', 'Tools.Bootstrap/MyBootstrap'); as noted in the readme.
- *
- * // DEPRECATED - since App::import also seems to be buggy, we better use
- * // CakePlugin::load('Tools', array('bootstrap' => true)); to include the bootstrap file
- * // directly.
- * // This file then can be ignored.
- */
-require(CakePlugin::path('Tools') . 'Config' . DS . 'bootstrap.php');

+ 49 - 0
Lib/Config/BaseEmailConfig.php

@@ -0,0 +1,49 @@
+<?php
+class BaseEmailConfig {
+
+	public $default = array(
+		'transport' => 'Smtp',
+	);
+
+	/**
+	 * Read Configure Email pwds and assign them to the configs.
+	 * Also assigns custom Mail config as well as log/trace configs.
+	 */
+	public function __construct() {
+		$pwds = (array)Configure::read('Email.Pwd');
+		foreach ($pwds as $key => $val) {
+			if (isset($this->{$key})) {
+				$this->{$key}['password'] = $val;
+			}
+		}
+
+		if (!empty($this->default['log'])) {
+			$this->default['report'] = true;
+		}
+		if (isset($this->default['log'])) {
+			unset($this->default['log']);
+		}
+		if (isset($this->default['trace'])) {
+			$this->default['log'] = 'email_trace';
+		}
+
+		if (Configure::read('debug') && !Configure::read('Email.live')) {
+			$this->default['transport'] = 'Debug';
+			if (!isset($this->default['trace'])) {
+				$this->default['log'] = 'email_trace';
+			}
+		}
+		if ($config = Configure::read('Mail')) {
+			if (!empty($config['smtp_host'])) {
+				$this->default['host'] = $config['smtp_host'];
+			}
+			if (!empty($config['smtp_username'])) {
+				$this->default['username'] = $config['smtp_username'];
+			}
+			if (!empty($config['smtp_password'])) {
+				$this->default['password'] = $config['smtp_password'];
+			}
+		}
+	}
+
+}

+ 11 - 8
Lib/EmailLib.php

@@ -9,16 +9,19 @@ if (!defined('BR')) {
 
 /**
  * Convenience class for internal mailer.
+ *
  * Adds some useful features and fixes some bugs:
  * - enable easier attachment adding (and also from blob)
  * - enable embedded images in html mails
  * - extensive logging and error tracing
  * - create mails with blob attachments (embedded or attached)
  * - allow wrapLength to be adjusted
- * - Configure::read('Config.x-mailer') can modify the x-mailer
+ * - Configure::read('Config.xMailer') can modify the x-mailer
  * - basic validation supported
  * - allow priority to be set (1 to 5)
  *
+ * Configs for auto-from can be set via Configure::read('Config.adminEmail').
+ *
  * @author Mark Scherer
  * @license MIT
  * @cakephp 2.x
@@ -58,8 +61,8 @@ class EmailLib extends CakeEmail {
 	public static function systemEmail($subject, $message = 'System Email', $transportConfig = null) {
 		$class = __CLASS__;
 		$instance = new $class($transportConfig);
-		$instance->from(Configure::read('Config.system_email'), Configure::read('Config.system_name'));
-		$instance->to(Configure::read('Config.admin_email'), Configure::read('Config.admin_name'));
+		$instance->from(Configure::read('Config.systemEmail'), Configure::read('Config.systemName'));
+		$instance->to(Configure::read('Config.adminEmail'), Configure::read('Config.adminName'));
 		if ($subject !== null) {
 			$instance->subject($subject);
 		}
@@ -562,15 +565,15 @@ class EmailLib extends CakeEmail {
 		$this->_error = null;
 		$this->_debug = null;
 
-		if ($fromEmail = Configure::read('Config.system_email')) {
-			$fromName = Configure::read('Config.system_name');
+		if ($fromEmail = Configure::read('Config.systemEmail')) {
+			$fromName = Configure::read('Config.systemName');
 		} else {
-			$fromEmail = Configure::read('Config.admin_email');
-			$fromName = Configure::read('Config.admin_name');
+			$fromEmail = Configure::read('Config.adminEmail');
+			$fromName = Configure::read('Config.adminName');
 		}
 		$this->from($fromEmail, $fromName);
 
-		if ($xMailer = Configure::read('Config.x-mailer')) {
+		if ($xMailer = Configure::read('Config.xMailer')) {
 			$this->addHeaders(array('X-Mailer' => $xMailer));
 		}
 	}

+ 134 - 0
Test/Case/Controller/Component/AuthExtComponentTest.php

@@ -0,0 +1,134 @@
+<?php
+App::uses('AuthExtComponent', 'Tools.Controller/Component');
+App::uses('Controller', 'Controller');
+App::uses('AppModel', 'Model');
+
+/**
+ * Test case for AuthExt
+ */
+class AuthExtComponentTest extends CakeTestCase {
+
+	public $fixtures = array('plugin.tools.tools_user', 'plugin.tools.role', 'core.cake_session');
+
+	public function setUp() {
+		parent::setUp();
+
+		Configure::delete('Auth');
+
+		$this->Controller = new AuthExtTestController(new CakeRequest, new CakeResponse);
+		$this->Controller->constructClasses();
+		$this->Controller->startupProcess();
+
+		$this->Controller->User->belongsTo = array(
+			'Role' => array(
+				'className' => 'Tools.Role'
+			)
+		);
+	}
+
+	public function tearDown() {
+		parent::tearDown();
+
+		unset($this->Controller->TestAuthExt);
+		unset($this->Controller);
+	}
+
+	/**
+	 * AuthExtComponentTest::testBasics()
+	 *
+	 * @return void
+	 */
+	public function testBasics() {
+		$res = $this->Controller->TestAuthExt->allow();
+
+		$is = $this->Controller->TestAuthExt->getModel();
+		$this->assertTrue(is_object($is) && $is->name === 'User');
+	}
+
+	/**
+	 * AuthExtComponentTest::_testCompleteAuth()
+	 *
+	 * @return void
+	 */
+	public function _testCompleteAuth() {
+		$is = $this->Controller->TestAuthExt->completeAuth(1);
+		debug($is);
+		$this->assertTrue(!empty($is));
+
+		$is = $this->Controller->TestAuthExt->completeAuth(111);
+		echo returns($is);
+		$this->assertFalse($is);
+	}
+
+}
+
+class TestAuthExtComponent extends AuthExtComponent {
+}
+
+class AuthExtTestController extends Controller {
+
+	public $uses = array('User');
+
+	/**
+	 * Components property
+	 *
+	 * @var array
+	 */
+	public $components = array('Session', 'TestAuthExt' => array('userModel' => 'AuthUser', 'parentModelAlias' => 'Role'));
+
+	/**
+	 * Failed property
+	 *
+	 * @var boolean
+	 */
+	public $failed = false;
+
+	/**
+	 * Used for keeping track of headers in test
+	 *
+	 * @var array
+	 */
+	public $testHeaders = array();
+
+	/**
+	 * Fail method
+	 *
+	 * @return void
+	 */
+	public function fail() {
+		$this->failed = true;
+	}
+
+	/**
+	 * Redirect method
+	 *
+	 * @param mixed $option
+	 * @param mixed $code
+	 * @param mixed $exit
+	 * @return void
+	 */
+	public function redirect($url, $status = null, $exit = true) {
+		return $status;
+	}
+
+	/**
+	 * Conveinence method for header()
+	 *
+	 * @param string $status
+	 * @return void
+	 */
+	public function header($status) {
+		$this->testHeaders[] = $status;
+	}
+
+}
+
+class AuthUser extends AppModel {
+
+	public $name = 'User';
+
+	public $alias = 'User';
+
+	public $useTable = 'tools_users';
+
+}

+ 15 - 13
Test/Case/Lib/EmailLibTest.php

@@ -3,7 +3,7 @@
 App::uses('MyCakeTestCase', 'Tools.TestSuite');
 App::uses('EmailLib', 'Tools.Lib');
 
-//Configure::write('Config.admin_email', '...');
+//Configure::write('Config.adminEmail', '...');
 
 class EmailLibTest extends MyCakeTestCase {
 
@@ -13,7 +13,7 @@ class EmailLibTest extends MyCakeTestCase {
 
 	public function setUp() {
 		parent::setUp();
-		$this->skipIf(!file_exists(APP . 'Config' . DS . 'email.php'), 'no email.php');
+		//$this->skipIf(!file_exists(APP . 'Config' . DS . 'email.php'), 'no email.php');
 
 		$this->Email = new TestEmailLib();
 	}
@@ -35,7 +35,7 @@ class EmailLibTest extends MyCakeTestCase {
 	 */
 	public function testSendDefault() {
 		// start
-		$this->Email->to(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
+		$this->Email->to(Configure::read('Config.adminEmail'), Configure::read('Config.adminEmailname'));
 		$this->Email->subject('Test Subject');
 
 		$res = $this->Email->send('xyz xyz');
@@ -48,11 +48,11 @@ class EmailLibTest extends MyCakeTestCase {
 
 		$this->Email->resetAndSet();
 		// start
-		$this->Email->to(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
+		$this->Email->to(Configure::read('Config.adminEmail'), Configure::read('Config.adminEmailname'));
 		$this->Email->subject('Test Subject 2');
-		$this->Email->template('default', 'internal');
+		$this->Email->template('default', 'default');
 		$this->Email->viewVars(array('x' => 'y', 'xx' => 'yy', 'text' => ''));
-		$this->Email->addAttachments(array(APP . 'webroot' . DS . 'img' . DS . 'icons' . DS . 'edit.gif'));
+		$this->Email->addAttachments(array(CakePlugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'img' . DS . 'edit.gif'));
 
 		$res = $this->Email->send('xyz');
 		// end
@@ -70,7 +70,7 @@ class EmailLibTest extends MyCakeTestCase {
 	 */
 	public function testSendFast() {
 		//$this->Email->resetAndSet();
-		//$this->Email->from(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
+		//$this->Email->from(Configure::read('Config.adminEmail'), Configure::read('Config.adminEmailname'));
 		$res = EmailLib::systemEmail('system-mail test', 'some fast email to admin test');
 		//debug($res);
 		$this->assertTrue($res);
@@ -93,7 +93,7 @@ class EmailLibTest extends MyCakeTestCase {
 		$result = $this->Email->getDebug();
 		$this->assertTextContains('X-Mailer: CakePHP Email', $result['headers']);
 
-		Configure::write('Config.x-mailer', 'Tools Plugin');
+		Configure::write('Config.xMailer', 'Tools Plugin');
 
 		$this->Email = new TestEmailLib();
 		$this->Email->from('cake@cakephp.org');
@@ -201,9 +201,9 @@ class EmailLibTest extends MyCakeTestCase {
 		$this->assertTrue(file_exists($file));
 		Configure::write('debug', 0);
 
-		$this->Email->to(Configure::read('Config.admin_email'));
+		$this->Email->to(Configure::read('Config.adminEmail'));
 		$this->Email->addAttachment($file);
-		$res = $this->Email->send('test_default', 'internal');
+		$res = $this->Email->send('test_default', 'default');
 		if ($error = $this->Email->getError()) {
 			$this->out($error);
 		}
@@ -211,7 +211,7 @@ class EmailLibTest extends MyCakeTestCase {
 		$this->assertTrue($res);
 
 		$this->Email->resetAndSet();
-		$this->Email->to(Configure::read('Config.admin_email'));
+		$this->Email->to(Configure::read('Config.adminEmail'));
 		$this->Email->addAttachment($file, 'x.jpg');
 		$res = $this->Email->send('test_custom_filename');
 
@@ -288,7 +288,7 @@ class EmailLibTest extends MyCakeTestCase {
 		Configure::write('debug', 0);
 		$this->Email = new TestEmailLib();
 		$this->Email->emailFormat('both');
-		$this->Email->to(Configure::read('Config.admin_email'));
+		$this->Email->to(Configure::read('Config.adminEmail'));
 		$cid = $this->Email->addEmbeddedAttachment($file);
 
 		$cid2 = $this->Email->addEmbeddedAttachment($file);
@@ -371,6 +371,8 @@ html-part
 	 * @return void
 	 */
 	public function testValidates() {
+		$this->skipIf(php_sapi_name() === 'cli', 'For now...');
+
 		$this->Email = new TestEmailLib();
 		$res = $this->Email->validates();
 		$this->assertFalse($res);
@@ -401,7 +403,7 @@ html-part
 		Configure::write('debug', 0);
 		$this->Email = new TestEmailLib();
 		$this->Email->emailFormat('both');
-		$this->Email->to(Configure::read('Config.admin_email'));
+		$this->Email->to(Configure::read('Config.adminEmail'));
 		$cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png', 'image/png');
 
 		$this->assertContains('@' . env('HTTP_HOST'), $cid);

+ 9 - 5
Test/Case/View/Helper/DatetimeHelperTest.php

@@ -19,6 +19,7 @@ class DatetimeHelperTest extends MyCakeTestCase {
 	/**
 	 * Test user age
 	 *
+	 * @return void
 	 */
 	public function testUserAge() {
 		$res = $this->Datetime->userAge('2010-01-01');
@@ -28,9 +29,9 @@ class DatetimeHelperTest extends MyCakeTestCase {
 	/**
 	 * Test cweek
 	 *
+	 * @return void
 	 */
 	public function testLengthOfTime() {
-
 		$this->assertEquals('6 ' . __('Minutes') . ', 40 ' . __('Seconds'), $this->Datetime->lengthOfTime(400));
 
 		$res = $this->Datetime->lengthOfTime(400, 'i');
@@ -41,13 +42,15 @@ class DatetimeHelperTest extends MyCakeTestCase {
 		//pr($res);
 		$this->assertEquals('6 ' . __('Days') . ', 0 ' . __('Hours'), $res);
 
-		#TODO: more
-
+		//TODO: more
 	}
 
+	/**
+	 * DatetimeHelperTest::testRelLengthOfTime()
+	 *
+	 * @return void
+	 */
 	public function testRelLengthOfTime() {
-		//echo $this->_header(__FUNCTION__);
-
 		$res = $this->Datetime->relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 3600));
 		//pr($res);
 		$this->assertTrue(!empty($res));
@@ -237,4 +240,5 @@ class DatetimeHelperTest extends MyCakeTestCase {
 
 		unset($this->Datetime);
 	}
+
 }

二进制
Test/test_files/img/edit.gif


+ 26 - 38
View/Helper/HcardHelper.php

@@ -8,7 +8,7 @@ App::uses('AppHelper', 'View/Helper');
  */
 class HcardHelper extends AppHelper {
 
-	public $data = array(
+	protected $_defaults = array(
 		'given_name' => 'Firstname',
 		'middle_name' => 'Middlename',
 		'family_name' => 'Lastname',
@@ -28,67 +28,55 @@ class HcardHelper extends AppHelper {
 	);
 
 	/**
-	 * TODO
+	 * @return string HTML
 	 */
-	public function addressFormatHtml($data, $prefix = false, $format = 'General') {
-		$data = $this->filter($data, $prefix);
-		$text = $this->style($data, $format);
+	public function addressFormatHtml($data = null, $format = 'General') {
+		if ($data === null) {
+			$data = $this->_defaults;
+		}
 		$text = '';
-		$text .= '<div id="hcard-' . $data['firstname'] . '-' . $data['lastname'] . '" class="vcard">';
-		$text .= '<span class="fn">' . $data['firstname'] . ' ' . $data['lastname'] . '</span>';
+		$text .= '<div id="hcard-' . $data['given_name'] . '-' . $data['family_name'] . '" class="vcard">';
+		$text .= '<span class="fn">' . $data['given_name'] . ' ' . $data['family_name'] . '</span>';
 		$text .= $this->address($data, $format);
 		$text .= '</div>';
 		return $text;
 	}
 
 	/**
-	 * TODO
+	 * @return string
 	 */
-	public function addressFormatRaw($data, $prefix = false, $format = 'General') {
-		$data = $this->filter($data, $prefix);
-		$text = $data['firstname'] . ' ' . $data['lastname'] . "\n";
-		$text .= $data['address'] . "\n";
-		if (Configure::read('Localization.address_format') === 'US') {
-			$text .= $data['city'] . ', ' . $data['state'] . ' ' . $data['postcode'] . "\n";
+	public function addressFormatRaw($data = null, $format = 'General') {
+		if ($data === null) {
+			$data = $this->_defaults;
+		}
+		$text = $data['given_name'] . ' ' . $data['family_name'] . "\n";
+		$text .= $data['street'] . "\n";
+		if (Configure::read('Localization.addressFormat') === 'US') {
+			$text .= $data['city'] . ', ' . $data['province'] . ' ' . $data['postal_code'] . "\n";
 		} else {
-			$text .= $data['postcode'] . ' ' . $data['city'] . "\n";
+			$text .= $data['postal_code'] . ' ' . $data['city'] . "\n";
 		}
 		$text .= $data['country'];
 		return $text;
 	}
 
 	/**
-	 * TODO
-	 */
-	public function style($data) {
-	}
-
-	/**
-	 * TODO
+	 * @return string
 	 */
 	public function address($data) {
+		if ($data === null) {
+			$data = $this->_defaults;
+		}
 		$text = '<div class="adr">';
-		$text .= '<div class="street-address">' . $data['address'] . '</div> ';
+		$text .= '<div class="street-address">' . $data['street'] . '</div> ';
 		$text .= '<span class="locality">' . $data['city'] . '</span>, ';
-		if (!empty($data['state'])) {
-			$text .= '<span class="region">' . $data['state'] . '</span> ';
+		if (!empty($data['province'])) {
+			$text .= '<span class="region">' . $data['province'] . '</span> ';
 		}
-		$text .= '<span class="postal-code">' . $data['postcode'] . '</span> ';
+		$text .= '<span class="postal-code">' . $data['postal_code'] . '</span> ';
 		$text .= '<span class="country-name">' . $data['country'] . '</span> ';
 		$text .= '</div>';
 		return $text;
 	}
 
-	/**
-	 * TODO
-	 */
-	public function filter($data, $prefix = '') {
-		if ($prefix) {
-			foreach ($data as $key => $row) {
-				$data[$prefix . $key] = $data[$key];
-			}
-		}
-		return $data;
-	}
-
 }