Permalink
Browse files

Add some more tests to name setting

  • Loading branch information...
1 parent 32c280b commit 23cb8fa531e3a191b5f39e60b7d911ed1af02edf @birtles birtles committed Mar 26, 2013
Showing with 43 additions and 13 deletions.
  1. +24 −11 wall/lib/walls.inc
  2. +19 −2 wall/tests/api/TestSetWall.php
View
@@ -242,7 +242,13 @@ class Wall {
// Sanitize values
switch($name) {
case 'name':
- // XXX
+ $value = wtrim($value);
+ // Don't validate the name if it is identical to the current wall name
+ // (which *ought* to be valid) since otherwise we'll get errors about
+ // the name being a duplicate.
+ if ($value !== $this->name) {
+ Wall::validateWallName($value);
+ }
break;
}
@@ -464,6 +470,22 @@ class Wall {
return $config['editor']['url'] . $path;
}
+ public static function validateWallName($name) {
+ // Check name has been trimmed
+ if ($name !== wtrim($name))
+ throw new KeyedException('bad-name');
+
+ // Check name is not empty
+ if (!strlen($name))
+ throw new KeyedException('empty-name');
+
+ // Check name is unique
+ if (!Walls::isNameUnique($name)) {
+ error_log("Wall with name '$name' already exists.");
+ throw new KeyedException('duplicate-name');
+ }
+ }
+
public function canAdminister() {
// In the future we will check if the wall has been shared with the current
// user or not
@@ -494,16 +516,7 @@ class Walls {
// Validate wall name
$name = wtrim($name);
- if (!strlen($name)) {
- error_log("Empty name");
- throw new KeyedException('empty-name');
- }
-
- // Check name is unique
- if (!Walls::isNameUnique($name)) {
- error_log("Wall with name '$name' already exists.");
- throw new KeyedException('duplicate-name');
- }
+ Wall::validateWallName($name);
// Validate design ID
$design = DesignGallery::getById($designId);
@@ -29,10 +29,24 @@ function testSetName() {
$wall = $this->getWall($wallId);
$this->assertEqual(@$wall['name'], 'ABCD');
- // Same title
+ // Try setting the same title--nothing should be returned since nothing
+ // changed
+ $result = $this->updateWall($wallId, array('name' => 'ABCD'));
+ $this->assertEqual(count($result), 0);
+
// Trimming
- // non-ASCII
+ $result = $this->updateWall($wallId, array('name' => " \twall name\n "));
+ $this->assertEqual(@$result['name'], 'wall name');
+
+ // Non-ASCII
+ $result = $this->updateWall($wallId, array('name' => 'テスト'));
+ $this->assertEqual(@$result['name'], 'テスト');
+
// Empty title
+ $result = $this->updateWall($wallId, array('name' => ""));
+ $this->assertTrue(@$result['error_key'] == 'empty-name',
+ "Made wall name empty");
+
// Whitespace only
// Duplicate title
@@ -51,4 +65,7 @@ function testUnrecognizedParams() {
function testNoChange() {
}
+
+ function testSetMultiple() {
+ }
}

0 comments on commit 23cb8fa

Please sign in to comment.