/** * Down **/ public function down() { if ($this->db->tableExists('#__assets')) { $rules = '{"core.admin":{"7":1},"core.manage":{"6":1},"core.view":[],"core.create":[],"core.delete":[],"core.edit":[],"core.edit.state":[],"core.edit.own":[]}'; $query = "SELECT id FROM `#__assets` WHERE `name` = 'com_answers' LIMIT 1"; $this->db->setQuery($query); $id = $this->db->loadResult(); if (!$id) { include_once PATH_CORE . DS . 'libraries' . DS . 'joomla' . DS . 'database' . DS . 'table' . DS . 'asset.php'; $tbl = new \JTableAsset($this->db); $tbl->level = 1; $tbl->parent = 1; $tbl->name = 'com_answers'; $tbl->title = 'com_answers'; $tbl->rules = $rules; $tbl->check(); $tbl->store(); } else { // Set the first zone as default $query = "UPDATE `#__assets` SET `rules` = " . $this->db->quote($rules) . " WHERE `id` = " . $this->db->quote($id); $this->db->setQuery($query); $this->db->query(); } } }
/** * Saves an asset * * @param JTableAsset $asset * @param string $name * @param int $parentId * @param string $title * @param array $rules * * @return int|boolean */ protected static function saveAsset($asset, $name, $parentId, $title, $rules) { $asset->parent_id = $parentId; $asset->name = $name; $asset->title = $title; $error = $asset->getError(); if ($error) { return false; } else { if (empty($asset->id) || $asset->parent_id != $parentId) { $asset->setLocation($parentId, 'last-child'); } $temp = array(); foreach ($rules as $ruleName => $rules) { $temp[$ruleName] = array(); foreach ($rules as $userGroup => $permission) { if ($permission !== "") { $temp[$ruleName][(int) $userGroup] = (int) $permission; } } } $asset->rules = json_encode($temp); if (!$asset->check() || !$asset->store()) { return false; } else { return $asset->id; } } }
/** * Tests JTableNested::store * * @return void * * @since 11.3 */ public function testStore() { // Use assets table since it does not override the JTableNested store() method $table = new JTableAsset(self::$dbo); // Existing row nulls=false $table->load('3'); $rules = $table->rules; $table->title = 'New Title'; $table->rules = null; $this->assertTrue($table->store(), 'Line: ' . __LINE__ . ' Table store should succeed'); $table->reset(); $table->load('3'); $this->assertEquals('New Title', $table->title, 'Line: ' . __LINE__ . ' Title should be updated'); $this->assertEquals($rules, $table->rules, 'Line: ' . __LINE__ . ' Rules should not be overwritten by null value'); // Existing Row nulls=true $table->rules = null; $table->title = 'New Title Null'; // $this->assertFalse($table->store(true), 'Line: '.__LINE__.' Table store should fail since rules field is not null in db'); // Dying here ^^^^ $table->reset(); $table->load('3'); $this->assertEquals('New Title', $table->title, 'Line: ' . __LINE__ . ' Title should not be updated'); $this->assertEquals($rules, $table->rules, 'Line: ' . __LINE__ . ' Rules should not be overwritten by null value'); // Existing Row with new parent (implicit move) // Move id 3 to be first child of 4 $table->reset(); $table->load('3'); $table->setLocation('4', 'first-child'); $table->title = 'Move 3 to first child of 4'; $this->assertTrue($table->store(), 'Line: ' . __LINE__ . ' Table store should succeed'); $treeTemp = $table->getTree('1'); $this->assertEquals('4', $treeTemp[2]->id, 'Line: ' . __LINE__ . ' id for element 2 should be 4'); $this->assertEquals('3', $treeTemp[3]->id, 'Line: ' . __LINE__ . ' id for element 3 should be 3'); $this->assertEquals(2, $treeTemp[3]->level, 'Line: ' . __LINE__ . ' level for element 3 should be 2'); // New row with reference node $table->reset(); $table->load('40'); $table->id = null; $table->title = 'New Node Last Child of 4'; $table->setLocation('4', 'last-child'); $table->name = 'com.banners.category.999'; // New row without reference node $this->assertTrue($table->store(), 'Line: ' . __LINE__ . ' Table store should succeed'); $treeTemp = $table->getTree('1'); $this->assertEquals(160, count($treeTemp), 'Line: ' . __LINE__ . ' Tree should now have 160 rows'); $this->assertEquals('com.banners.category.999', $treeTemp[6]->name, 'Line: ' . __LINE__ . ' New node should be in position 6'); $this->assertEquals('4', $treeTemp[6]->parent_id, 'Line: ' . __LINE__ . ' New node should parent id of 4'); $this->assertEquals(2, $treeTemp[6]->level, 'Line: ' . __LINE__ . ' New node level should be 2'); // Test _location_id == 0 $table->reset(); $table->load('40'); $table->id = null; $table->title = "Test Location 0"; $table->setLocation('0', 'last-child'); $table->name = 'com.banners.category.998'; $this->assertTrue($table->store(), 'Line: ' . __LINE__ . ' Table store should succeed'); $treeTemp = $table->getTree('1'); $this->assertEquals('Test Location 0', $treeTemp[160]->title, 'Line: ' . __LINE__ . ' New node should be in last position (160)'); // Check lock $lockedTable = $this->getMock('JTableCategory', array('_lock'), array(&self::$dbo)); $lockedTable->expects($this->any())->method('_lock')->will($this->returnValue(false)); $this->assertFalse($lockedTable->store('999'), 'Line: ' . __LINE__ . ' Delete should fail because cannot lock'); }