From 5f99c81035523e097a54fa6290de7b233c6691c3 Mon Sep 17 00:00:00 2001 From: JoostK Date: Sun, 9 Sep 2012 20:52:33 +0200 Subject: [PATCH 1/8] Fixed a problem whith `Eloquent::get_dirty` When you had a synched Eloquent model (e.g. without changed values) but one of those values is `null`, then that value would be considered as dirty. `Eloquent::changed` returns false, but the value is present in `Eloquent::get_dirty`. This fix makes sure that a `null` value in `$attributes` is only present in `get_dirty` when it wasn't at all *set* in `$original`. --- laravel/database/eloquent/model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index 306d471a..645937f5 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -518,7 +518,7 @@ abstract class Model { foreach ($this->attributes as $key => $value) { - if ( ! isset($this->original[$key]) or $value !== $this->original[$key]) + if ( ! array_key_exists($key, $this->original) or $value != $this->original[$key]) { $dirty[$key] = $value; } From 147e0ec656e9aadc9683ed58a88f286224047d0c Mon Sep 17 00:00:00 2001 From: JoostK Date: Mon, 10 Sep 2012 00:51:02 +0200 Subject: [PATCH 2/8] Implementation of Eloquent tests Basic Eloquent unit tests. Lacks database operations, relationship testing and some magic method calls. --- laravel/tests/application/models/model.php | 15 ++ laravel/tests/cases/eloquent.test.php | 292 +++++++++++++++++++++ 2 files changed, 307 insertions(+) create mode 100644 laravel/tests/application/models/model.php create mode 100644 laravel/tests/cases/eloquent.test.php diff --git a/laravel/tests/application/models/model.php b/laravel/tests/application/models/model.php new file mode 100644 index 00000000..cd2c82bd --- /dev/null +++ b/laravel/tests/application/models/model.php @@ -0,0 +1,15 @@ +set_attribute('setter', 'setter: '.$setter); + } + + public function get_getter() + { + return 'getter: '.$this->get_attribute('getter'); + } + +} \ No newline at end of file diff --git a/laravel/tests/cases/eloquent.test.php b/laravel/tests/cases/eloquent.test.php new file mode 100644 index 00000000..ac489f39 --- /dev/null +++ b/laravel/tests/cases/eloquent.test.php @@ -0,0 +1,292 @@ + 'Taylor', 'age' => 25, 'setter' => 'foo'); + + $model = new Model($array); + + $this->assertEquals('Taylor', $model->name); + $this->assertEquals(25, $model->age); + $this->assertEquals('setter: foo', $model->setter); + } + + /** + * Test the Model::fill method. + * + * @group laravel + */ + public function testAttributesAreSetByFillMethod() + { + $array = array('name' => 'Taylor', 'age' => 25, 'setter' => 'foo'); + + $model = new Model(); + $model->fill($array); + + $this->assertEquals('Taylor', $model->name); + $this->assertEquals(25, $model->age); + $this->assertEquals('setter: foo', $model->setter); + } + + /** + * Test the Model::fill_raw method. + * + * @group laravel + */ + public function testAttributesAreSetByFillRawMethod() + { + $array = array('name' => 'Taylor', 'age' => 25, 'setter' => 'foo'); + + $model = new Model(); + $model->fill_raw($array); + + $this->assertEquals($array, $model->attributes); + } + + /** + * Test the Model::fill method with accessible. + * + * @group laravel + */ + public function testAttributesAreSetByFillMethodWithAccessible() + { + Model::$accessible = array('name', 'age'); + + $array = array('name' => 'Taylor', 'age' => 25, 'foo' => 'bar'); + + $model = new Model(); + $model->fill($array); + + $this->assertEquals('Taylor', $model->name); + $this->assertEquals(25, $model->age); + $this->assertNull($model->foo); + + Model::$accessible = null; + } + + /** + * Test the Model::fill method with empty accessible array. + * + * @group laravel + */ + public function testAttributesAreSetByFillMethodWithEmptyAccessible() + { + Model::$accessible = array(); + + $array = array('name' => 'Taylor', 'age' => 25, 'foo' => 'bar'); + + $model = new Model(); + $model->fill($array); + + $this->assertEquals(array(), $model->attributes); + $this->assertNull($model->name); + $this->assertNull($model->age); + $this->assertNull($model->foo); + + Model::$accessible = null; + } + + /** + * Test the Model::fill_raw method with accessible. + * + * @group laravel + */ + public function testAttributesAreSetByFillRawMethodWithAccessible() + { + Model::$accessible = array('name', 'age'); + + $array = array('name' => 'taylor', 'age' => 25, 'setter' => 'foo'); + + $model = new Model(); + $model->fill_raw($array); + + $this->assertEquals($array, $model->attributes); + + Model::$accessible = null; + } + + /** + * Test the Model::__set method. + * + * @group laravel + */ + public function testAttributeMagicSetterMethodChangesAttribute() + { + Model::$accessible = array('setter'); + + $array = array('setter' => 'foo', 'getter' => 'bar'); + + $model = new Model($array); + $model->setter = 'bar'; + $model->getter = 'foo'; + + $this->assertEquals('setter: bar', $model->get_attribute('setter')); + $this->assertEquals('foo', $model->get_attribute('getter')); + + Model::$accessible = null; + } + + /** + * Test the Model::__get method. + * + * @group laravel + */ + public function testAttributeMagicGetterMethodReturnsAttribute() + { + $array = array('setter' => 'foo', 'getter' => 'bar'); + + $model = new Model($array); + + $this->assertEquals('setter: foo', $model->setter); + $this->assertEquals('getter: bar', $model->getter); + } + + /** + * Test the Model::set_* method. + * + * @group laravel + */ + public function testAttributeSetterMethodChangesAttribute() + { + Model::$accessible = array('setter'); + + $array = array('setter' => 'foo', 'getter' => 'bar'); + + $model = new Model($array); + $model->set_setter('bar'); + $model->set_getter('foo'); + + $this->assertEquals('setter: bar', $model->get_attribute('setter')); + $this->assertEquals('foo', $model->get_attribute('getter')); + + Model::$accessible = null; + } + + /** + * Test the Model::get_* method. + * + * @group laravel + */ + public function testAttributeGetterMethodReturnsAttribute() + { + $array = array('setter' => 'foo', 'getter' => 'bar'); + + $model = new Model($array); + + $this->assertEquals('setter: foo', $model->get_setter()); + $this->assertEquals('getter: bar', $model->get_getter()); + } + + /** + * Test determination of dirty/changed attributes. + * + * @group laravel + */ + public function testDeterminationOfChangedAttributes() + { + $array = array('name' => 'Taylor', 'age' => 25, 'foo' => null); + + $model = new Model($array, true); + $model->name = 'Otwell'; + $model->new = null; + + $this->assertTrue($model->changed('name')); + $this->assertFalse($model->changed('age')); + $this->assertFalse($model->changed('foo')); + $this->assertFalse($model->changed('new')); + $this->assertTrue($model->dirty()); + $this->assertEquals(array('name' => 'Otwell', 'new' => null), $model->get_dirty()); + + $model->sync(); + + $this->assertFalse($model->changed('name')); + $this->assertFalse($model->changed('age')); + $this->assertFalse($model->changed('foo')); + $this->assertFalse($model->changed('new')); + $this->assertFalse($model->dirty()); + $this->assertEquals(array(), $model->get_dirty()); + } + + /** + * Test the Model::purge method. + * + * @group laravel + */ + public function testAttributePurge() + { + $array = array('name' => 'Taylor', 'age' => 25); + + $model = new Model($array); + $model->name = 'Otwell'; + $model->age = 26; + + $model->purge('name'); + + $this->assertFalse($model->changed('name')); + $this->assertNull($model->name); + $this->assertTrue($model->changed('age')); + $this->assertEquals(26, $model->age); + $this->assertEquals(array('age' => 26), $model->get_dirty()); + } + + /** + * Test the Model::table method. + * + * @group laravel + */ + public function testTableMethodReturnsCorrectName() + { + $model = new Model(); + $this->assertEquals('models', $model->table()); + + Model::$table = 'table'; + $this->assertEquals('table', $model->table()); + + Model::$table = null; + $this->assertEquals('models', $model->table()); + } + + /** + * Test the Model::to_array method. + * + * @group laravel + */ + public function testConvertingToArray() + { + Model::$hidden = array('password', 'hidden'); + + $array = array('name' => 'Taylor', 'age' => 25, 'password' => 'laravel', 'null' => null); + + $model = new Model($array); + + $first = new Model(array('first' => 'foo', 'password' => 'hidden')); + $second = new Model(array('second' => 'bar', 'password' => 'hidden')); + $third = new Model(array('third' => 'baz', 'password' => 'hidden')); + + $model->relationships['one'] = new Model(array('foo' => 'bar', 'password' => 'hidden')); + $model->relationships['many'] = array($first, $second, $third); + $model->relationships['hidden'] = new Model(array('should' => 'visible')); + $model->relationships['null'] = null; + + $this->assertEquals(array( + 'name' => 'Taylor', 'age' => 25, 'null' => null, + 'one' => array('foo' => 'bar'), + 'many' => array( + array('first' => 'foo'), + array('second' => 'bar'), + array('third' => 'baz'), + ), + 'hidden' => array('should' => 'visible'), + 'null' => null, + ), $model->to_array()); + + } + +} \ No newline at end of file From 01ba355787c7f5df9d92ca1148faa21526abb6eb Mon Sep 17 00:00:00 2001 From: Bryan te Beek Date: Mon, 10 Sep 2012 21:11:20 +0200 Subject: [PATCH 3/8] Optimize Str-helper (see pull request #1180) Signed-off-by: Bryan te Beek --- laravel/str.php | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/laravel/str.php b/laravel/str.php index 23f2b731..68130042 100644 --- a/laravel/str.php +++ b/laravel/str.php @@ -9,17 +9,22 @@ class Str { */ public static $pluralizer; - /** - * Get the default string encoding for the application. - * - * This method is simply a short-cut to Config::get('application.encoding'). - * - * @return string - */ - public static function encoding() - { - return Config::get('application.encoding'); - } + /** + * Cache application encoding locally to save expensive calls to Config::get(). + * + * @var string + */ + public static $encoding = null; + + /** + * Get the appliction.encoding without needing to request it from Config::get() each time. + * + * @return string + */ + protected static function encoding() + { + return static::$encoding ?: static::$encoding = Config::get('application.encoding'); + } /** * Get the length of a string. From 03acf9ca504deb0bab5ff8ca978cd45937304688 Mon Sep 17 00:00:00 2001 From: Aaron Kuzemchak Date: Sat, 15 Sep 2012 18:57:29 -0400 Subject: [PATCH 4/8] Fixes language URI routing issue Signed-off-by: Aaron Kuzemchak --- laravel/laravel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laravel/laravel.php b/laravel/laravel.php index 74c387f9..20ec8057 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -137,7 +137,7 @@ $languages[] = Config::get('application.language'); foreach ($languages as $language) { - if (starts_with($uri, $language)) + if (preg_match("#^{$language}(/.*)?$#i", $uri)) { Config::set('application.language', $language); From 581fcc5c47946882e560dd08c43a5005491fffe4 Mon Sep 17 00:00:00 2001 From: Aaron Kuzemchak Date: Sat, 15 Sep 2012 20:48:18 -0400 Subject: [PATCH 5/8] Cleaner regex Signed-off-by: Aaron Kuzemchak --- laravel/laravel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laravel/laravel.php b/laravel/laravel.php index 20ec8057..3e382b89 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -137,7 +137,7 @@ $languages[] = Config::get('application.language'); foreach ($languages as $language) { - if (preg_match("#^{$language}(/.*)?$#i", $uri)) + if (preg_match("#^{$language}(?:$|/)#i", $uri)) { Config::set('application.language', $language); From 3d51200a882d5a557dd14c88f1ae34f539b353b6 Mon Sep 17 00:00:00 2001 From: Joel Marcotte Date: Thu, 20 Sep 2012 17:54:28 -0400 Subject: [PATCH 6/8] Auth token now nulled on logout Signed-off-by: Joel Marcotte --- laravel/auth/drivers/driver.php | 2 ++ laravel/tests/cases/auth.test.php | 3 --- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/laravel/auth/drivers/driver.php b/laravel/auth/drivers/driver.php index db649d24..96e90637 100644 --- a/laravel/auth/drivers/driver.php +++ b/laravel/auth/drivers/driver.php @@ -127,6 +127,8 @@ abstract class Driver { $this->cookie($this->recaller(), null, -2000); Session::forget($this->token()); + + $this->token = null; } /** diff --git a/laravel/tests/cases/auth.test.php b/laravel/tests/cases/auth.test.php index 16ba428a..f038a176 100644 --- a/laravel/tests/cases/auth.test.php +++ b/laravel/tests/cases/auth.test.php @@ -294,9 +294,6 @@ class AuthTest extends PHPUnit_Framework_TestCase { Auth::logout(); - // A workaround since Cookie will is only stored in memory, until Response class is called. - Auth::driver()->token = null; - $this->assertNull(Auth::user()); $this->assertFalse(isset(Session::$instance->session['data']['laravel_auth_drivers_fluent_login'])); From 258169ea0000a8b90f5d56b7102c2212e0bad612 Mon Sep 17 00:00:00 2001 From: RK Date: Mon, 24 Sep 2012 09:58:20 -0400 Subject: [PATCH 7/8] [#1261] get_key now pulls from $original instead This is in reference to issue #1261, where Model->get_key() returns the key from the $attributes instead of from the $original property. This breaks the functionality of a model with a primary key that may change, as the SQL generated will be something like: UPDATE `model` SET `key` = 'new-key' WHERE `key` = 'new-key'; Which won't update the model in the database. --- laravel/database/eloquent/model.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index c2566496..c57b36ad 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -544,7 +544,7 @@ abstract class Model { */ public function get_key() { - return $this->get_attribute(static::$key); + return get_array($this->original, static::$key); } /** @@ -721,7 +721,7 @@ abstract class Model { { if (array_key_exists($key, $this->$source)) return true; } - + if (method_exists($this, $key)) return true; } From f148f6211c378864c34c19f0aebcac97832aa9fe Mon Sep 17 00:00:00 2001 From: RK Date: Mon, 24 Sep 2012 10:35:07 -0400 Subject: [PATCH 8/8] Fixing the array_get misspelling. --- laravel/database/eloquent/model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index c57b36ad..b1ee5176 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -544,7 +544,7 @@ abstract class Model { */ public function get_key() { - return get_array($this->original, static::$key); + return array_get($this->original, static::$key); } /**