浏览代码

Fix SluggedBehavior with multiple labels.

Mark Scherer 10 年之前
父节点
当前提交
5f0245690d
共有 2 个文件被更改,包括 32 次插入6 次删除
  1. 5 3
      src/Model/Behavior/SluggedBehavior.php
  2. 27 3
      tests/TestCase/Model/Behavior/SluggedBehaviorTest.php

+ 5 - 3
src/Model/Behavior/SluggedBehavior.php

@@ -180,7 +180,8 @@ class SluggedBehavior extends Behavior {
 	/**
 	 * SluggedBehavior::slug()
 	 *
-	 * @param Entity $entity
+	 * @param \Cake\ORM\Entity $entity Entity
+	 * @param array $options Options
 	 * @return void
 	 */
 	public function slug(Entity $entity, array $options = []) {
@@ -191,12 +192,13 @@ class SluggedBehavior extends Behavior {
 		if ($overwrite || $entity->isNew() || !$entity->get($this->_config['field'])) {
 			$slug = [];
 			foreach ((array)$this->_config['label'] as $v) {
-				$v = $this->generateSlug($entity->get($v), $entity);
-				if ($v) {
+				$v = $entity->get($v);
+				if ($v !== null && $v !== '') {
 					$slug[] = $v;
 				}
 			}
 			$slug = implode($slug, $this->_config['separator']);
+			$slug = $this->generateSlug($slug, $entity);
 			$entity->set($this->_config['field'], $slug);
 		}
 	}

+ 27 - 3
tests/TestCase/Model/Behavior/SluggedBehaviorTest.php

@@ -83,6 +83,22 @@ class SluggedBehaviorTest extends TestCase {
 		//debug($result);
 	}
 
+	/**
+	 * @return void
+	 */
+	public function testAddUniqueMultipleLabels() {
+		$this->articles->behaviors()->Slugged->config('label', ''); // Hack necessary right now to avoid title showing up twice
+		$this->articles->behaviors()->Slugged->config(['mode' => 'ascii', 'unique' => true, 'label' => ['title', 'long_title']]);
+
+		$entity = $this->_getEntity(null, null, ['long_title' => 'blae']);
+		$result = $this->articles->save($entity);
+		$this->assertEquals('test-123-blae', $result->get('slug'));
+
+		$entity = $this->_getEntity(null, null, ['long_title' => 'blä']);
+		$result = $this->articles->save($entity);
+		$this->assertEquals('test-123-blae-1', $result->get('slug'));
+	}
+
 /**
  * SluggedBehaviorTest::testCustomFinder()
  *
@@ -612,11 +628,19 @@ class SluggedBehaviorTest extends TestCase {
  *
  * @return Entity
  */
-	protected function _getEntity($title = 'test 123', $field = 'title', array $options = []) {
+	protected function _getEntity($title = null, $field = null, array $data = [], array $options = []) {
 		$options += ['validate' => false];
-		return new Entity([
+		if ($title === null) {
+			$title = 'test 123';
+		}
+		if ($field === null) {
+			$field = 'title';
+		}
+
+		$data = [
 			$field => $title
-		], $options);
+		] + $data;
+		return new Entity($data, $options);
 	}
 
 }