Browse Source

Merge pull request #155 from dereuromark/master-fixes

Fix SluggedBehavior with multiple labels.
Mark S. 10 years ago
parent
commit
16b1869c0a

+ 5 - 78
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);
 		}
 	}
@@ -227,61 +229,6 @@ class SluggedBehavior extends Behavior {
 	}
 
 	/**
-	 * Generate slug method
-	 *
-	 * If a new row, or overwrite is set to true, check for a change to a label field and add the slug to the data
-	 * to be saved
-	 *
-	 * @deprecated Not in use anymore!
-	 * @return void
-	 */
-	public function _slug(Entity $entity) {
-		extract($this->_config);
-
-		$overwrite = $this->config['overwrite'];
-		if (!$overwrite && !$entity->get($overwriteField)) {
-			$overwrite = true;
-		}
-		if ($overwrite || $entity->isNew()) {
-			if ($label) {
-				$somethingToDo = false;
-				foreach ($label as $field) {
-					$alias = $this->_table->alias();
-					if (strpos($field, '.') !== false) {
-						list($alias, $field) = explode('.', $field, 2);
-					}
-					if (isset($Model->data[$alias][$field])) {
-						$somethingToDo = true;
-					}
-				}
-				if (!$somethingToDo) {
-					return;
-				}
-				$slug = [];
-				foreach ($label as $field) {
-					$alias = $this->_table->alias();
-					if (strpos($field, '.')) {
-						list($alias, $field) = explode('.', $field);
-					}
-					if (isset($Model->data[$alias][$field])) {
-						if (is_array($Model->data[$alias][$field])) {
-							return $this->_multiSlug($Model);
-						}
-						$slug[] = $Model->data[$alias][$field];
-					} elseif ($Model->id) {
-						$slug[] = $Model->field($field);
-					}
-				}
-				$slug = implode($slug, $separator);
-			} else {
-				$slug = $this->display($Model);
-			}
-			$slug = $Model->slug($slug);
-			$Model->data[$this->_table->alias()][$slugField] = $slug;
-		}
-	}
-
-	/**
 	 * Slug method
 	 *
 	 * For the given string, generate a slug. The replacements used are based on the mode setting, If tidy is false
@@ -374,26 +321,6 @@ class SluggedBehavior extends Behavior {
 	}
 
 	/**
-	 * Display method
-	 *
-	 * Cheat - use find('list') and assume it has been modified such that lists show in the desired format.
-	 * First check (since this method is called in beforeSave) if there is data to be saved, and use that
-	 * to get the display name
-	 * Otherwise, read from the database
-	 *
-	 * @deprecated Not in use anymore!
-	 * @param mixed $id
-	 * @return mixed string (the display name) or false
-	 */
-	public function display($id) {
-		$conditions = array_merge([
-			$this->_table->alias() . '.' . $this->_table->primaryKey() => $id],
-			$this->_config['scope']);
-		$record = $this->_table->find('first', ['conditions' => $conditions]);
-		return $record->get($this->_table->displayField());
-	}
-
-	/**
 	 * ResetSlugs method.
 	 *
 	 * Regenerate all slugs. On large dbs this can take more than 30 seconds - a time

+ 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->configShallow(['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);
 	}
 
 }