浏览代码

Fix length issue

euromark 11 年之前
父节点
当前提交
276bf8a325
共有 2 个文件被更改,包括 53 次插入15 次删除
  1. 12 13
      Model/Behavior/SluggedBehavior.php
  2. 41 2
      Test/TestCase/Model/Behavior/SluggedBehaviorTest.php

+ 12 - 13
Model/Behavior/SluggedBehavior.php

@@ -24,24 +24,26 @@ use Cake\Error\Exception;
 class SluggedBehavior extends Behavior {
 
 	/**
-	 * Default settings
+	 * Default config
 	 *
-	 * label
+	 * - length
+	 *  Set to 0 for no length. Will be auto-detected if possible via schema.
+	 * - label
 	 * 	set to the name of a field to use for the slug, an array of fields to use as slugs or leave as null to rely
 	 * 	on the format returned by find('list') to determine the string to use for slugs
-	 * overwrite has 2 values
+	 * - overwrite has 2 values
 	 * 	false - once the slug has been saved, do not change it (use if you are doing lookups based on slugs)
 	 * 	true - if the label field values change, regenerate the slug (use if you are the slug is just window-dressing)
-	 * unique has 2 values
+	 * - unique has 2 values
 	 * 	false - will not enforce a unique slug, whatever the label is is direclty slugged without checking for duplicates
 	 * 	true - use if you are doing lookups based on slugs (see overwrite)
-	 * mode has the following values
+	 * - mode has the following values
 	 * 	ascii - retuns an ascii slug generated using the core Inflector::slug() function
 	 * 	display - a dummy mode which returns a slug legal for display - removes illegal (not unprintable) characters
 	 * 	url - returns a slug appropriate to put in a URL
 	 * 	class - a dummy mode which returns a slug appropriate to put in a html class (there are no restrictions)
 	 * 	id - retuns a slug appropriate to use in a html id
-	 * case has the following values
+	 * - case has the following values
 	 * 	null - don't change the case of the slug
 	 * 	low - force lower case. E.g. "this-is-the-slug"
 	 * 	up - force upper case E.g. "THIS-IS-THE-SLUG"
@@ -92,12 +94,9 @@ class SluggedBehavior extends Behavior {
 
 		parent::__construct($table, $config);
 
-		if (!$this->_config['length']) {
+		if ($this->_config['length'] === null) {
 			$length = $table->schema()->column($this->_config['field'])['length'];
-			if (!$length) {
-				$length = 255;
-			}
-			$this->_config['length'] = $length;
+			$this->_config['length'] = $length ?: 0;
 		}
 
 		$this->_table = $table;
@@ -255,7 +254,7 @@ class SluggedBehavior extends Behavior {
 				$slug = 'x' . $slug;
 			}
 		}
-		if (strlen($slug) > $this->_config['length']) {
+		if ($this->_config['length'] & (strlen($slug) > $this->_config['length'])) {
 			$slug = mb_substr($slug, 0, $this->_config['length']);
 			while ($slug && strlen($slug) > $this->_config['length']) {
 				$slug = mb_substr($slug, 0, mb_strlen($slug) - 1);
@@ -295,7 +294,7 @@ class SluggedBehavior extends Behavior {
 			while ($this->_table->exists($conditions)) {
 				$i++;
 				$suffix	= $separator . $i;
-				if (strlen($slug . $suffix) > $this->_config['length']) {
+				if ($this->_config['length'] && (strlen($slug . $suffix) > $this->_config['length'])) {
 					$slug = substr($slug, 0, $this->_config['length'] - strlen($suffix));
 				}
 				$conditions[$field] = $slug . $suffix;

+ 41 - 2
Test/TestCase/Model/Behavior/SluggedBehaviorTest.php

@@ -96,13 +96,52 @@ class SluggedBehaviorTest extends TestCase {
 	}
 
 /**
+ * Length based on manual config.
+ *
+ * @return void
+ */
+	public function testLengthRestrictionManual() {
+		$this->articles->addBehavior('Tools.Slugged', ['length' => 155]);
+		$entity = $this->_getEntity(str_repeat('foo bar ', 100));
+
+		$result = $this->articles->save($entity);
+		$this->assertEquals(155, strlen($result->get('slug')));
+	}
+
+/**
+ * Length based on auto-detect of schema.
+ *
+ * @return void
+ */
+	public function testLengthRestrictionAutoDetect() {
+		$this->articles->addBehavior('Tools.Slugged');
+		$entity = $this->_getEntity(str_repeat('foo bar ', 100));
+
+		$result = $this->articles->save($entity);
+		$this->assertEquals(255, strlen($result->get('slug')));
+	}
+
+/**
+ * Ensure that you can overwrite length.
+ *
+ * @return void
+ */
+	public function testLengthRestrictionNoLimit() {
+		$this->articles->addBehavior('Tools.Slugged', ['length' => 0]);
+		$entity = $this->_getEntity(str_repeat('foo bar ', 100));
+		debug(strlen($entity->get('slug')));
+		$result = $this->articles->save($entity);
+		$this->assertEquals(799, strlen($result->get('slug')));
+	}
+
+/**
  * Get a new Entity
  *
  * @return Entity
  */
-	protected function _getEntity() {
+	protected function _getEntity($title = 'test 123') {
 		return new Entity([
-			'title' => 'test 123'
+			'title' => $title
 		]);
 	}