<commit_before><?php
/**
 * Test Sluggable behavior
 * @author <NAME>
 * @group Sluggable
 */
class Garp_Model_Behavior_SluggableTest extends Garp_Test_PHPUnit_TestCase {

	public function testShouldAutoGenerateSlug() {
		$model = $this->_getConfiguredModel(array(
			'baseField' => 'name'
		));

		$model->insert(array('name' => '<NAME>'));
		$row = $model->fetchRow();

		$this->assertEquals('henk-jan-de-beuker', $row->slug);
	}

	public function testShouldIncrementSlug() {
		$model = new Mocks_Model_SluggableTest();
		$model->registerObserver(new Garp_Model_Behavior_Sluggable(array(
			'baseField' => 'name'
		)));

		$model->insert(array('name' => '<NAME>'));
		$model->insert(array('name' => '<NAME>'));
		$row = $model->fetchRow($model->select()->order('id DESC'));

		$this->assertEquals('henk-jan-de-beuker-2', $row->slug);
	}

	public function testShouldGenerateMultipleSlugs() {
		$model = $this->_getConfiguredModel(array(
			'baseField' => array('name', 'address'),
			'slugField' => array('slug', 'slug2')
		));

		$model->insert(array(
			'name' => '<NAME>',
			'address' => 'beukenlaan 20'
		));
		$row = $model->fetchRow();

		$this->assertEquals('henk-jan-de-beuker', $row->slug);
		$this->assertEquals('beukenlaan-20', $row->slug2);
	}

	public function testShouldGenerateSlugFromMultipleFields() {
		$model = $this->_getConfiguredModel(array(
			'baseField' => array('name', 'address'),
			'slugField' => 'slug'
		));

		$model->insert(array(
			'name' => '<NAME>',
			'address' => 'beukenlaan 20'
		));
		$row = $model->fetchRow();

		$this->assertEquals('henk-jan-de-beuker-beukenlaan-20', $row->slug);
	}

	public function testShouldGenerateSlugForMultilingualModel() {
		Zend_Controller_Front::getInstance()->setParam('locales', array('nl', 'en'));

		$model = new Mocks_Model_Sluggable2Test();
		// Save only Dutch name, slug should appear in both Dutch and English records
		$model->insert(array(
			'name' => array('nl' => '<NAME>'),
		));

		$modelNl = new Mocks_Model_Sluggable2TestNl();
		$row = $modelNl->fetchRow();
		$this->assertEquals('henk-jan-de-beuker', $row->slug);

		$modelEn = new Mocks_Model_Sluggable2TestEn();
		$row = $modelEn->fetchRow();
		$this->assertEquals('henk-jan-de-beuker', $row->slug);
	}

	/**
 	 * A known bug occurred in the past when a second-language row
 	 * would be updated after it already existed in the primary language.
 	 * It would then not receive a new slug in the second language, if the 
 	 * baseField would not be specified in the second language. 
 	 * For instance: a record about <NAME> would receive the slug "michael-jackson" if 
 	 * the name is <NAME>. The second language doesn't have to 
 	 * update the name, but only the description. However, this would fail to
 	 * create the slug.
 	 * This test checks for that.
 	 */
	public function testShouldGenerateSlugForMultilingualModelAfterUpdate() {
		Zend_Controller_Front::getInstance()->setParam('locales', array('nl', 'en'));

		$model = new Mocks_Model_Sluggable2Test();
		// Save primary language first
		$id = $model->insert(array(
			'name' => array('nl' => '<NAME>'),
			'tag' => array('nl' => 'nl__nl'),
			'something' => 'abc'
		));
		// Update with secondary language
		$model->update(array(
			'tag' => array('en' => 'en__en'),
			'something' => 'def' // <-- needed to generate valid UPDATE query... >.<
		), "`id` = '$id'");

		$i18nModel = new Mocks_Model_Sluggable2TestI18n();
		$row = $i18nModel->fetchRow(
			$i18nModel->select()->where('lang = "en"')->where('_sluggable_test_2_id = ?', $id)
		);
		// @note: since its the second rendition of the record, the slug will be incremented
		$this->assertEquals('henk-jan-de-beuker-2', $row->slug);
	}	

	public function testShouldGenerateSlugWithDate() {
		$model = $this->_getConfiguredModel(array(
			'baseField' => array(
				array(
					'column' => 'd',
					'type' => 'date'
				),
				'name'
			)
		));

		$model->insert(array('name' => '<NAME>', 'd' => '2013-10-12'));
		$row = $model->fetchRow();

		$this->assertEquals('12-10-2013-henk-jan-de-beuker', $row->slug); 
	}

	public function testShouldGenerateSlugWithFormattedDate() {
		$model = $this->_getConfiguredModel(array(
			'baseField' => array(
				array(
					'column' => 'd',
 				   	'format' => 'Y',
					'type' => 'date'
				),
				'name'
			)
		));

		$model->insert(array('name' => '<NAME>', 'd' => '2013-10-12'));
		$row = $model->fetchRow();

		$this->assertEquals('2013-henk-jan-de-beuker', $row->slug);
	}

	protected function _getConfiguredModel($config) {
		$model = new Mocks_Model_SluggableTest();
		$model->registerObserver(new Garp_Model_Behavior_Sluggable($config));
		return $model;
	}

	/**
 	 * Setup db tables
 	 */
	public function setUp() {
		Garp_Cache_Manager::purge();
		$dbAdapter = $this->getDatabaseAdapter();

		$dbAdapter->query('SET foreign_key_checks = 0;');
		$dbAdapter->query('DROP TABLE IF EXISTS `_sluggable_test`;');
		$dbAdapter->query('
		CREATE TABLE `_sluggable_test`(
			`id` int UNSIGNED NOT NULL AUTO_INCREMENT,
			`name` varchar(124) NULL,<commit_msg>Unit tests for Sluggable behavior<commit_after><?php
/**
 * Test Sluggable behavior
 * @author <NAME>
 * @group Sluggable
 */
class Garp_Model_Behavior_SluggableTest extends Garp_Test_PHPUnit_TestCase {

	public function testShouldAutoGenerateSlug() {
		$model = $this->_getConfiguredModel(array(
			'baseField' => 'name'
		));

		$model->insert(array('name' => '<NAME>'));
		$row = $model->fetchRow();

		$this->assertEquals('henk-jan-de-beuker', $row->slug);
	}

	public function testShouldIncrementSlug() {
		$model = new Mocks_Model_SluggableTest();
		$model->registerObserver(new Garp_Model_Behavior_Sluggable(array(
			'baseField' => 'name'
		)));

		$model->insert(array('name' => '<NAME>'));
		$model->insert(array('name' => '<NAME>'));
		$row = $model->fetchRow($model->select()->order('id DESC'));

		$this->assertEquals('henk-jan-de-beuker-2', $row->slug);
	}

	public function testShouldGenerateMultipleSlugs() {
		$model = $this->_getConfiguredModel(array(
			'baseField' => array('name', 'address'),
			'slugField' => array('slug', 'slug2')
		));

		$model->insert(array(
			'name' => '<NAME>',
			'address' => 'beukenlaan 20'
		));
		$row = $model->fetchRow();

		$this->assertEquals('henk-jan-de-beuker', $row->slug);
		$this->assertEquals('beukenlaan-20', $row->slug2);
	}

	public function testShouldGenerateSlugFromMultipleFields() {
		$model = $this->_getConfiguredModel(array(
			'baseField' => array('name', 'address'),
			'slugField' => 'slug'
		));

		$model->insert(array(
			'name' => '<NAME>',
			'address' => 'beukenlaan 20'
		));
		$row = $model->fetchRow();

		$this->assertEquals('henk-jan-de-beuker-beukenlaan-20', $row->slug);
	}

	public function testShouldGenerateSlugForMultilingualModel() {
		Zend_Controller_Front::getInstance()->setParam('locales', array('nl', 'en'));

		$model = new Mocks_Model_Sluggable2Test();
		// Save only Dutch name, slug should appear in both Dutch and English records
		$model->insert(array(
			'name' => array('nl' => '<NAME>'),
		));

		$modelNl = new Mocks_Model_Sluggable2TestNl();
		$row = $modelNl->fetchRow();
		$this->assertEquals('henk-jan-de-beuker', $row->slug);

		$modelEn = new Mocks_Model_Sluggable2TestEn();
		$row = $modelEn->fetchRow();
		$this->assertEquals('henk-jan-de-beuker', $row->slug);
	}

	/**
 	 * A known bug occurred in the past when a second-language row
 	 * would be updated after it already existed in the primary language.
 	 * It would then not receive a new slug in the second language, if the 
 	 * baseField would not be specified in the second language. 
 	 * For instance: a record about <NAME> would receive the slug "michael-jackson" if 
 	 * the name is <NAME>. The second language doesn't have to 
 	 * update the name, but only the description. However, this would fail to
 	 * create the slug.
 	 * This test checks for that.
 	 */
	public function testShouldGenerateSlugForMultilingualModelAfterUpdate() {
		Zend_Controller_Front::getInstance()->setParam('locales', array('nl', 'en'));

		$model = new Mocks_Model_Sluggable2Test();
		// Save primary language first
		$id = $model->insert(array(
			'name' => array('nl' => '<NAME>'),
			'tag' => array('nl' => 'nl__nl'),
			'something' => 'abc'
		));
		// Update with secondary language
		$model->update(array(
			'tag' => array('en' => 'en__en'),
			'something' => 'def' // <-- needed to generate valid UPDATE query... >.<
		), "`id` = '$id'");

		$i18nModel = new Mocks_Model_Sluggable2TestI18n();
		$row = $i18nModel->fetchRow(
			$i18nModel->select()->where('lang = "en"')->where('_sluggable_test_2_id = ?', $id)
		);
		// @note: since its the second rendition of the record, the slug will be incremented
		$this->assertEquals('henk-jan-de-beuker-2', $row->slug);
	}	

	public function testShouldGenerateSlugWithDate() {
		$model = $this->_getConfiguredModel(array(
			'baseField' => array(
				array(
					'column' => 'd',
					'type' => 'date'
				),
				'name'
			)
		));

		$model->insert(array('name' => '<NAME>', 'd' => '2013-10-12'));
		$row = $model->fetchRow();

		$this->assertEquals('12-10-2013-henk-jan-de-beuker', $row->slug); 
	}

	public function testShouldGenerateSlugWithFormattedDate() {
		$model = $this->_getConfiguredModel(array(
			'baseField' => array(
				array(
					'column' => 'd',
 				   	'format' => 'Y',
					'type' => 'date'
				),
				'name'
			)
		));

		$model->insert(array('name' => '<NAME>', 'd' => '2013-10-12'));
		$row = $model->fetchRow();

		$this->assertEquals('2013-henk-jan-de-beuker', $row->slug); 
	}

	public function testShouldIncrementSlugWithFormattedDate() {
		$model = $this->_getConfiguredModel(array(
			'baseField' => array(
				array(
					'column' => 'd',
					'type' => 'date'
				),
				'name'
			)
		));

		$model->insert(array('name' => '<NAME>', 'd' => '2013-10-12'));
		$model->insert(array('name' => '<NAME>', 'd' => '2013-10-12'));
		$row = $model->fetchRow($model->select()->order('id DESC'));

		$this->assertEquals('12-10-2013-henk-jan-de-beuker-2', $row->slug);
	}

	protected function _getConfiguredModel($config) {
		$model = new Mocks_Model_SluggableTest();
		$model->registerObserver(new Garp_Model_Behavior_Sluggable($config));
		return $model;
	}

	/**
 	 * Setup db tables
 	 */
	public function setUp() {
		Garp_Cache_Manager::purge();
		$dbAdapter = $this->getDatabaseAdapter();

		$dbAdapter->query('SET foreign_key_checks = 0;');
		$dbAdapter->query('DROP TABLE IF EXISTS `_sluggable_test`;');
		$dbAdapter->query('
		CREATE TABLE `_sluggable_test`(
			`id` int UNSIGNED NOT NULL AUTO_INCREMENT,
			`name` varchar(124) NULL,
{'fraction_non_alphanumeric': 0.18706523782665915, 'fraction_numerical': 0.013254371122391428, 'mean_word_length': 4.210088148873654, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 228, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3952291', 'n_tokens_mistral': 4204, 'n_tokens_neox': 3710, 'n_words': 755}