Index: inc/core/class.dc.core.php
===================================================================
--- inc/core/class.dc.core.php	(revision 3036)
+++ inc/core/class.dc.core.php	(revision 3064)
@@ -4,5 +4,5 @@
 # This file is part of Dotclear 2.
 #
-# Copyright (c) 2003-2014 Olivier Meunier & Association Dotclear
+# Copyright (c) 2003-2015 Olivier Meunier & Association Dotclear
 # Licensed under the GPL version 2.0 license.
 # See LICENSE file or
@@ -10,5 +10,4 @@
 #
 # -- END LICENSE BLOCK -----------------------------------------
-if (!defined('DC_RC_PATH')) { return; }
 
 /**
Index: inc/public/class.dc.template.php
===================================================================
--- inc/public/class.dc.template.php	(revision 3063)
+++ inc/public/class.dc.template.php	(revision 3064)
@@ -4,5 +4,5 @@
 # This file is part of Dotclear 2.
 #
-# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
+# Copyright (c) 2003-2015 Olivier Meunier & Association Dotclear
 # Licensed under the GPL version 2.0 license.
 # See LICENSE file or
@@ -10,5 +10,4 @@
 #
 # -- END LICENSE BLOCK -----------------------------------------
-if (!defined('DC_RC_PATH')) { return; }
 
 class dcTemplate extends template
@@ -345,5 +344,5 @@
 					$alias[$k] = array();
 				}
-				if (!is_array($v)) {
+				if (!isset($default_alias[$k]) || !is_array($default_alias[$k])) {
 					$default_alias[$k] = array();
 				}
Index: tests/unit/bootstrap.php
===================================================================
--- tests/unit/bootstrap.php	(revision 2999)
+++ tests/unit/bootstrap.php	(revision 3064)
@@ -23,4 +23,6 @@
 $__autoload['pgsqlConnection'] = CLEARBRICKS_PATH.'/dblayer/class.pgsql.php';
 $__autoload['record'] = CLEARBRICKS_PATH.'/dblayer/dblayer.php';
+$__autoload['template'] = CLEARBRICKS_PATH.'/template/class.template.php';
+$__autoload['path'] = CLEARBRICKS_PATH.'/common/lib.files.php';
 
 $__autoload['dcCore'] = __DIR__.'/../../inc/core/class.dc.core.php';
Index: tests/unit/inc/public/class.dc.template.php
===================================================================
--- tests/unit/inc/public/class.dc.template.php	(revision 3064)
+++ tests/unit/inc/public/class.dc.template.php	(revision 3064)
@@ -0,0 +1,129 @@
+<?php
+# -- BEGIN LICENSE BLOCK ---------------------------------------
+#
+# This file is part of Dotclear 2.
+#
+# Copyright (c) 2003-2015 Olivier Meunier & Association Dotclear
+# Licensed under the GPL version 2.0 license.
+# See LICENSE file or
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+#
+# -- END LICENSE BLOCK -----------------------------------------
+
+namespace tests\unit;
+
+use atoum;
+
+require_once __DIR__.'/../../bootstrap.php';
+$f = str_replace('\\','/',__FILE__);
+require_once(str_replace('tests/unit/','',$f));
+
+class dcTemplate extends atoum
+{
+	private function getCore() {
+		$controller = new \atoum\mock\controller();
+		$controller->__construct = function() {};
+
+		$core = new \mock\dcCore(null,null,null,null,null,null,$controller);
+		$core->blog = json_decode('{"settings": {"system": {"tpl_allow_php":false,"tpl_use_cache":true}}}',false);
+		return $core;
+	}
+
+	public function testGetSortByStrWithDefaultValues() {
+		// copy from ../../../../inc/public/class.dc.template.php:getSortByStr()
+		$default_alias = array(
+			'post' => array(
+				'title' => 'post_title',
+				'selected' => 'post_selected',
+				'author' => 'user_id',
+				'date' => 'post_dt',
+				'id' => 'post_id',
+				'comment' => 'nb_comment',
+				'trackback' => 'nb_trackback'
+			),
+			'comment' => array(
+				'author' => 'comment_author',
+				'date' => 'comment_dt',
+				'id' => 'comment_id'
+			)
+		);
+
+		$tpl = new \dcTemplate(sys_get_temp_dir(),'$tpl',$this->getCore());
+
+		foreach ($default_alias as $table => $fields) {
+			$this
+				->string($tpl->getSortByStr(array(),$table))
+				->isEqualTo($default_alias[$table]['date'].' desc');
+			foreach ($fields as $field => $value) {
+				$this
+					->string($tpl->getSortByStr(array('sortby' => $field),$table))
+					->isEqualTo($value.' desc');
+				$this
+					->string($tpl->getSortByStr(array('sortby' => $field,'order' => 'asc'),$table))
+					->isEqualTo($value.' asc');
+			}
+		}
+	}
+
+	public function testGetSortByStrWithNonExistingKeyAndExistingTable() {
+		$tpl = new \dcTemplate(sys_get_temp_dir(),'$tpl',$this->getCore());
+		$this
+			->string($tpl->getSortByStr(array('sortby' => 'dummy_field'),'post'))
+			->isEqualTo('post_dt desc')
+			->string($tpl->getSortByStr(array('sortby' => 'dummy_field'),'comment'))
+			->isEqualTo('comment_dt desc');
+	}
+
+	public function testGetSortByStrWithNewKeyAndExistingTable() {
+		$new_alias = array();
+		$new_alias['post']['category'] = 'cat_id';
+		$new_alias['post']['format'] = 'post_format';
+		$new_alias['comment']['filter'] = 'spam_filter';
+
+		$core = $this->getCore();
+		$core->addBehavior('templateCustomSortByAlias',
+						   function($alias) use ($new_alias) {
+							   foreach ($new_alias as $table => $fields) {
+								   foreach ($fields as $field => $value) {
+									   $alias[$table][$field] = $value;
+								   }
+							   }
+						   }
+		);
+
+		$tpl = new \dcTemplate(sys_get_temp_dir(),'$tpl',$core);
+		foreach ($new_alias as $table => $fields) {
+			foreach ($fields as $field => $value) {
+				$this
+					->string($tpl->getSortByStr(array('sortby' => $field),$table))
+					->isEqualTo($value.' desc');
+			}
+		}
+	}
+
+	public function testGetSortByStrWithNewTable() {
+		$new_alias = array();
+		$new_alias['dummy_table']['field1'] = 'field1_id';
+		$new_alias['dummy_table']['field2'] = 'field2_format';
+
+		$core = $this->getCore();
+		$core->addBehavior('templateCustomSortByAlias',
+						   function($alias) use ($new_alias) {
+							   foreach ($new_alias as $table => $fields) {
+								   foreach ($fields as $field => $value) {
+									   $alias[$table][$field] = $value;
+								   }
+							   }
+						   }
+		);
+
+		$tpl = new \dcTemplate(sys_get_temp_dir(),'$tpl',$core);
+		foreach ($new_alias as $table => $fields) {
+			foreach ($fields as $field => $value) {
+				$this
+					->string($tpl->getSortByStr(array('sortby' => $field),$table))
+					->isEqualTo($value.' desc');
+			}
+		}
+	}
+}
