public function testValidation() { $contact = $this->contacts('testAnyone'); $tag = new Tags(); $tag->setAttributes(array('itemId' => $contact->id, 'type' => get_class($contact), 'itemName' => $contact->name, 'tag' => 'test', 'taggedBy' => 'testuser')); $this->assertSaves($tag); // ensure that normalization was performed upon validation $this->assertEquals(Tags::normalizeTag('test'), $tag->tag); // ensure that tag must be unique $tag = new Tags(); $tag->setAttributes(array('itemId' => $contact->id, 'type' => get_class($contact), 'itemName' => $contact->name, 'tag' => 'test', 'taggedBy' => 'testuser')); $tag->validate(); $this->assertTrue($tag->hasErrors('tag')); // ensure that tag must be unique $tag = new Tags(); $tag->setAttributes(array('itemId' => $contact->id, 'type' => get_class($contact), 'itemName' => $contact->name, 'tag' => '#test', 'taggedBy' => 'testuser')); $tag->validate(); $this->assertTrue($tag->hasErrors('tag')); // ensure that tag must be unique $tag = new Tags(); $tag->setAttributes(array('itemId' => $contact->id, 'type' => get_class($contact), 'itemName' => $contact->name, 'tag' => '#test,', 'taggedBy' => 'testuser')); $tag->validate(); $this->assertTrue($tag->hasErrors('tag')); }
public static function normalizeTags(array $tags, $suppressHash = false) { foreach ($tags as &$tag) { $tag = Tags::normalizeTag($tag, $suppressHash); } return $tags; }
/** * Adds the specified tag(s) to the owner model, but not * if the tag has already been added. * @param mixed $tags a string or array of strings containing tags * @return boolean whether or not at least one tag was added successfully */ public function addTags($tags) { $result = false; $addedTags = array(); foreach ((array) $tags as $tagName) { if (empty($tagName)) { continue; } if (!$this->hasTag($tagName)) { // check for duplicate tag $tag = new Tags(); $tag->tag = Tags::normalizeTag($tagName); $tag->itemId = $this->getOwner()->id; $tag->type = get_class($this->getOwner()); $tag->taggedBy = Yii::app()->getSuName(); $tag->timestamp = time(); $tag->itemName = $this->getOwner()->name; if ($tag->save()) { $this->_tags[] = $tag->tag; // update tag cache $addedTags[] = $tagName; $result = true; } else { throw new CHttpException(422, 'Failed saving tag due to errors: ' . json_encode($tag->errors)); } } } if ($this->flowTriggersEnabled) { X2Flow::trigger('RecordTagAddTrigger', array('model' => $this->getOwner(), 'tags' => $addedTags)); } return $result; }