Skip to content

Commit

Permalink
Merge pull request #1339 from sjinks/tests
Browse files Browse the repository at this point in the history
Fix bug in Phalcon\Tag\Select::selectField()
  • Loading branch information
Phalcon committed Oct 7, 2013
2 parents 6cd15bf + d2de4fd commit 768111d
Show file tree
Hide file tree
Showing 29 changed files with 676 additions and 568 deletions.
7 changes: 4 additions & 3 deletions ext/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ static void phalcon_tag_get_escaper(zval **return_value_ptr, zval *params TSRMLS
*return_value_ptr = result;
}

static zend_bool phalcon_tag_associative_only(HashTable *ht, void *pData, zend_hash_key *hash_key, void *pParam)
static zend_bool phalcon_tag_attribute_filter(HashTable *ht, void *pData, zend_hash_key *hash_key, void *pParam)
{
return hash_key->arKey && hash_key->nKeyLength;
zval **z = (zval**)pData;
return hash_key->arKey && hash_key->nKeyLength && Z_TYPE_PP(z) != IS_ARRAY;
}

PHALCON_STATIC void phalcon_tag_render_attributes(zval *code, zval *attributes TSRMLS_DC)
Expand Down Expand Up @@ -152,7 +153,7 @@ PHALCON_STATIC void phalcon_tag_render_attributes(zval *code, zval *attributes T
}
}

zend_hash_merge_ex(Z_ARRVAL_P(attrs), Z_ARRVAL_P(attributes), (copy_ctor_func_t)zval_add_ref, sizeof(zval*), phalcon_tag_associative_only, NULL);
zend_hash_merge_ex(Z_ARRVAL_P(attrs), Z_ARRVAL_P(attributes), (copy_ctor_func_t)zval_add_ref, sizeof(zval*), phalcon_tag_attribute_filter, NULL);

if (phalcon_array_isset_string(attrs, SS("escape"))) {
phalcon_array_unset_string(&attrs, SS("escape"), 0);
Expand Down
57 changes: 25 additions & 32 deletions ext/tag/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ PHALCON_INIT_CLASS(Phalcon_Tag_Select){
*/
PHP_METHOD(Phalcon_Tag_Select, selectField){

zval *parameters, *data = NULL, *params = NULL, *eol, *id = NULL, *name, *value = NULL;
zval *parameters, *data = NULL, *params = NULL, *id = NULL, *name, *value = NULL;
zval *use_empty = NULL, *empty_value = NULL, *empty_text = NULL, *code;
zval *close_option, *options = NULL, *using;
zval *close_option, *options = NULL, *using = NULL;
zval *resultset_options, *array_options;

PHALCON_MM_GROW();
Expand All @@ -90,8 +90,6 @@ PHP_METHOD(Phalcon_Tag_Select, selectField){
PHALCON_CPY_WRT(params, parameters);
}

PHALCON_INIT_VAR(eol);
ZVAL_STRING(eol, PHP_EOL, 1);
if (!phalcon_array_isset_long(params, 0)) {
PHALCON_OBS_VAR(id);
phalcon_array_fetch_string(&id, params, SL("id"), PH_NOISY);
Expand Down Expand Up @@ -133,7 +131,7 @@ PHP_METHOD(Phalcon_Tag_Select, selectField){
if (phalcon_array_isset_string(params, SS("useEmpty"))) {
if (!phalcon_array_isset_string(params, SS("emptyValue"))) {
PHALCON_INIT_VAR(empty_value);
ZVAL_STRING(empty_value, "", 1);
ZVAL_EMPTY_STRING(empty_value);
} else {
PHALCON_OBS_NVAR(empty_value);
phalcon_array_fetch_string(&empty_value, params, SL("emptyValue"), PH_NOISY);
Expand All @@ -153,11 +151,15 @@ PHP_METHOD(Phalcon_Tag_Select, selectField){
phalcon_array_unset_string(&params, SS("useEmpty"), PH_SEPARATE);
}

if (phalcon_array_isset_string_fetch(&using, params, SS("using"))) {
phalcon_array_unset_string(&params, SS("using"), PH_SEPARATE);
}

PHALCON_INIT_VAR(code);
ZVAL_STRING(code, "<select", 1);
phalcon_tag_render_attributes(code, params TSRMLS_CC);

PHALCON_SCONCAT_SV(code, ">", eol);
phalcon_concat_self_str(&code, SL(">" PHP_EOL) TSRMLS_CC);

PHALCON_INIT_VAR(close_option);
ZVAL_STRING(close_option, "</option>" PHP_EOL, 1);
Expand All @@ -166,7 +168,6 @@ PHP_METHOD(Phalcon_Tag_Select, selectField){
* Create an empty value
*/
PHALCON_SCONCAT_SVSVV(code, "\t<option value=\"", empty_value, "\">", empty_text, close_option);
phalcon_array_unset_string(&params, SS("useEmpty"), PH_SEPARATE);
}

if (phalcon_array_isset_long(params, 1)) {
Expand All @@ -175,44 +176,37 @@ PHP_METHOD(Phalcon_Tag_Select, selectField){
} else {
PHALCON_CPY_WRT(options, data);
}

if (Z_TYPE_P(options) == IS_OBJECT) {

/**
* The options is a resultset
*/
if (!phalcon_array_isset_string(params, SS("using"))) {
if (using == NULL) {
PHALCON_THROW_EXCEPTION_STR(phalcon_tag_exception_ce, "The 'using' parameter is required");
return;
} else {
PHALCON_OBS_VAR(using);
phalcon_array_fetch_string(&using, params, SL("using"), PH_NOISY);
if (Z_TYPE_P(using) != IS_ARRAY) {
if (Z_TYPE_P(using) != IS_OBJECT) {
PHALCON_THROW_EXCEPTION_STR(phalcon_tag_exception_ce, "The 'using' parameter should be an Array");
return;
}
}
}


if (Z_TYPE_P(using) != IS_ARRAY && Z_TYPE_P(using) != IS_OBJECT) {
PHALCON_THROW_EXCEPTION_STR(phalcon_tag_exception_ce, "The 'using' parameter should be an Array");
return;
}

/**
* Create the SELECT's option from a resultset
*/
PHALCON_INIT_VAR(resultset_options);
phalcon_call_self_p4(resultset_options, this_ptr, "_optionsfromresultset", options, using, value, close_option);
phalcon_concat_self(&code, resultset_options TSRMLS_CC);
} else if (Z_TYPE_P(options) == IS_ARRAY) {
/**
* Create the SELECT's option from an array
*/
PHALCON_INIT_VAR(array_options);
phalcon_call_self_p3(array_options, this_ptr, "_optionsfromarray", options, value, close_option);
phalcon_concat_self(&code, array_options TSRMLS_CC);
} else {
if (Z_TYPE_P(options) == IS_ARRAY) {
/**
* Create the SELECT's option from an array
*/
PHALCON_INIT_VAR(array_options);
phalcon_call_self_p3(array_options, this_ptr, "_optionsfromarray", options, value, close_option);
phalcon_concat_self(&code, array_options TSRMLS_CC);
} else {
PHALCON_THROW_EXCEPTION_STR(phalcon_tag_exception_ce, "Invalid data provided to SELECT helper");
return;
}
PHALCON_THROW_EXCEPTION_STR(phalcon_tag_exception_ce, "Invalid data provided to SELECT helper");
return;
}

phalcon_concat_self_str(&code, SL("</select>") TSRMLS_CC);
Expand Down Expand Up @@ -248,7 +242,6 @@ PHP_METHOD(Phalcon_Tag_Select, _optionsFromResultset){
PHALCON_INIT_VAR(escaped);

while (1) {

PHALCON_INIT_NVAR(r0);
phalcon_call_method(r0, resultset, "valid");
if (PHALCON_IS_NOT_FALSE(r0)) {
Expand Down
24 changes: 24 additions & 0 deletions ext/tests/issue-741.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Segmentation fault when calling Phalcon\Validation\Message\Group::offsetSet() with a non-object - https://github.com/phalcon/cphalcon/issues/741
--SKIPIF--
<?php include('skipif.inc'); ?>
--FILE--
<?php
$group = new \Phalcon\Validation\Message\Group;
try {
$group[0] = 'invalid';
}
catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}

try {
$group->appendMessage('invalid');
}
catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
The message must be an object
The message must be an object
16 changes: 16 additions & 0 deletions ext/tests/issue-743.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Segmentation Fault in Phalcon\Validation::bind() - https://github.com/phalcon/cphalcon/pull/743
--SKIPIF--
<?php include('skipif.inc'); ?>
--FILE--
<?php
$v = new \Phalcon\Validation;
try {
$v->bind(0, 0);
}
catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
The entity must be an object
47 changes: 47 additions & 0 deletions ext/tests/issue-744.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
Segmentation Fault in Phalcon\Tag::setDI() and setDefaults() - https://github.com/phalcon/cphalcon/pull/744
--SKIPIF--
<?php include('skipif.inc'); ?>
--FILE--
<?php
class DIDescendant extends Phalcon\DI {}

$v = new \Phalcon\Tag();

try {
$v->setDI(0);
assert(false);
}
catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}

/* This won't work — we do not validate DI yet :-(
try {
$v->setDI(new \stdClass());
assert(false);
}
catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
*/

$v->setDI(new \Phalcon\DI());
echo "OK", PHP_EOL;

$v->setDI(new \DIDescendant());
echo "OK", PHP_EOL;

try {
$v->setDefaults(0);
assert(false);
}
catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
Parameter dependencyInjector must be an Object
OK
OK
An array is required as default values
1 change: 1 addition & 0 deletions php-tests/library/Phalcon/Test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.db.local.php
107 changes: 60 additions & 47 deletions php-tests/library/Phalcon/Test/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,65 @@

class Config
{
public static function init()
{
$config = array(
'db' => array(
'mysql' => array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'phalcon_test',
),
'postgresql' => array(
'host' => '127.0.0.1',
'username' => 'postgres',
'password' => '',
'dbname' => 'phalcon_test',
'schema' => 'public',
),
'sqlite' => array(
'host' => '',
'username' => '',
'password' => '',
'dbname' => '/tmp/phalcon_test.sqlite',
),
),
'tr' => array(
'en' => array(
'hi' => 'Hello',
'bye' => 'Good Bye',
'hello-key' => 'Hello %name%',
'song-key' => 'This song is %song% (%artist%)',
),
'es' => array(
'hi' => 'Hola',
'bye' => 'Adiós',
'hello-key' => 'Hola %name%',
'song-key' => 'La canción es %song% (%artist%)',
),
'fr' => array(
'hi' => 'Bonjour',
'bye' => 'Au revoir',
'hello-key' => 'Bonjour %name%',
'song-key' => 'La chanson est %song% (%artist%)',
),
),
);
public static function init()
{
if (file_exists(__DIR__ . '/config.db.local.php')) {
$db_config = require (__DIR__ . '/config.db.local.php');
}
else {
$db_config = array(
'mysql' => array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'phalcon_test',
),
'postgresql' => array(
'host' => '127.0.0.1',
'username' => 'postgres',
'password' => '',
'dbname' => 'phalcon_test',
'schema' => 'public',
),
'sqlite' => array(
'host' => '',
'username' => '',
'password' => '',
'dbname' => '/tmp/phalcon_test.sqlite',
),
);
}

return $config;
}
$config = array(
'db' => $db_config,
'tr' => array(
'en' => array(
'hi' => 'Hello',
'bye' => 'Good Bye',
'hello-key' => 'Hello %name%',
'song-key' => 'This song is %song% (%artist%)',
),
'es' => array(
'hi' => 'Hola',
'bye' => 'Adiós',
'hello-key' => 'Hola %name%',
'song-key' => 'La canción es %song% (%artist%)',
),
'fr' => array(
'hi' => 'Bonjour',
'bye' => 'Au revoir',
'hello-key' => 'Bonjour %name%',
'song-key' => 'La chanson est %song% (%artist%)',
),
'hi' => array(
'hi' => 'हाय',
'bye' => 'अलविदा',
'hello-key' => 'हाय %name%',
'song-key' => 'यह गीत %song% (%artist%) है',
),
),
);

return $config;
}
}
Loading

0 comments on commit 768111d

Please sign in to comment.