Browse Source

Manual save of slug.

euromark 11 years ago
parent
commit
7662da328f

+ 1 - 1
src/Model/Behavior/SluggedBehavior.php

@@ -180,7 +180,7 @@ class SluggedBehavior extends Behavior {
 		if (!$overwrite && $entity->get($this->_config['overwriteField'])) {
 			$overwrite = true;
 		}
-		if ($overwrite || $entity->isNew()) {
+		if ($overwrite || $entity->isNew() || $entity->get($this->_config['field']) === null) {
 			$slug = array();
 			foreach ((array)$this->_config['label'] as $v) {
 				$v = $this->generateSlug($entity->get($v), $entity);

+ 19 - 0
tests/TestCase/Model/Behavior/SluggedBehaviorTest.php

@@ -93,6 +93,25 @@ class SluggedBehaviorTest extends TestCase {
 		$this->assertEquals('Foo', $article->get('title'));
 	}
 
+	/**
+	 * Tests that manual slugging works.
+	 *
+	 * @return void
+	 */
+	public function testSlugManualSave() {
+		$article = $this->articles->newEntity(array('title' => 'Some Cool String'));
+		$result = $this->articles->save($article);
+		$this->assertEquals('Some-Cool-String', $result['slug']);
+
+		$this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => 'foo-bar']);
+		$result = $this->articles->save($article);
+		$this->assertEquals('foo-bar', $result['slug']);
+
+		$this->articles->patchEntity($article, ['title' => 'Some Cool Other String', 'slug' => 'foo-bar-bat']);
+		$result = $this->articles->save($article);
+		$this->assertEquals('foo-bar-bat', $result['slug']);
+	}
+
 /**
  * Length based on manual config.
  *