Skip to content
Snippets Groups Projects
Commit b0ac608e authored by Andreas Gohr's avatar Andreas Gohr
Browse files

Merge branch 'fix-2087' of git://github.com/phy25/dokuwiki into pull-request-2091

* 'fix-2087' of git://github.com/phy25/dokuwiki:
  Fix p_set_metadata damaging contributors with numeric ID
  Add tests for array_replace part of set_metadata
parents 2e3f2c78 2d102494
No related branches found
No related tags found
No related merge requests found
<?php
/**
* Unit Test for inc/common.php - pageinfo()
*
* @author Christopher Smith <chris@jalakai.co.uk>
*/
class parserutils_set_metadata_test extends DokuWikiTest {
// the id used for this test case
private $id;
function helper_prepare_users($id = 1){
global $INFO, $USERINFO;
$_SERVER['REMOTE_ADDR'] = '1.2.3.4';
if($id == 2){
$USERINFO = array(
'pass' => '179ad45c6ce2cb97cf1029e212046e81',
'name' => 'Tester2',
'mail' => 'tester2@example.com',
'grps' => array ('user'),
);
$INFO['userinfo'] = $USERINFO;
$_SERVER['REMOTE_USER'] = 'tester2';
}else{
global $USERINFO, $INFO;
$USERINFO = array(
'pass' => '179ad45c6ce2cb97cf1029e212046e81',
'name' => 'Tester1',
'mail' => 'tester1@example.com',
'grps' => array ('admin','user'),
);
$INFO['userinfo'] = $USERINFO;
$_SERVER['REMOTE_USER'] = '1';
}
}
/**
* test array merge, including contributors with numeric keys and array data overwritting
*/
function test_array_replace(){
// prepare user
$this->helper_prepare_users(1);
// prepare page
$this->id = 'test:set_metadata_array_replace';
saveWikiText($this->id, 'Test', 'Test data setup');
$meta = p_get_metadata($this->id);
$this->assertEquals('1', $meta['user'], 'Initial page has wrong user ID');
// $this->assertEquals(empty($meta['contributor']), true, 'Initial page should have no contributors');
// first revision with numeric user
saveWikiText($this->id, 'Test1', 'Test first edit');
$meta = p_get_metadata($this->id);
$last_edit_date = $meta['date']['modified'];
$this->assertEquals(array('1'=>'Tester1'), $meta['contributor'], 'First edit contributors error');
// second revision with alphabetic user
sleep(1); // To generate a different timestamp
$this->helper_prepare_users(2);
saveWikiText($this->id, 'Test2', 'Test second edit');
$meta = p_get_metadata($this->id);
$this->assertNotEquals($last_edit_date, $meta['date']['modified'], 'First edit date merge error');
$this->assertEquals(array('tester2'=>'Tester2', '1'=>'Tester1'), $meta['contributor'], 'Second edit contributors error');
// third revision with the first user
$this->helper_prepare_users(1);
saveWikiText($this->id, 'Test3', 'Test third edit');
$meta = p_get_metadata($this->id);
$this->assertEquals(array('tester2'=>'Tester2', '1'=>'Tester1'), $meta['contributor'], 'Third edit contributors error');
}
}
//Setup VIM: ex: et ts=4 :
......@@ -337,13 +337,13 @@ function p_set_metadata($id, $data, $render=false, $persistent=true){
foreach ($value as $subkey => $subvalue){
if(isset($meta['current'][$key][$subkey]) && is_array($meta['current'][$key][$subkey])) {
$meta['current'][$key][$subkey] = array_merge($meta['current'][$key][$subkey], (array)$subvalue);
$meta['current'][$key][$subkey] = array_replace($meta['current'][$key][$subkey], (array)$subvalue);
} else {
$meta['current'][$key][$subkey] = $subvalue;
}
if($persistent) {
if(isset($meta['persistent'][$key][$subkey]) && is_array($meta['persistent'][$key][$subkey])) {
$meta['persistent'][$key][$subkey] = array_merge($meta['persistent'][$key][$subkey], (array)$subvalue);
$meta['persistent'][$key][$subkey] = array_replace($meta['persistent'][$key][$subkey], (array)$subvalue);
} else {
$meta['persistent'][$key][$subkey] = $subvalue;
}
......@@ -355,10 +355,10 @@ function p_set_metadata($id, $data, $render=false, $persistent=true){
// these keys, must have subkeys - a legitimate value must be an array
if (is_array($value)) {
$meta['current'][$key] = !empty($meta['current'][$key]) ? array_merge((array)$meta['current'][$key],$value) : $value;
$meta['current'][$key] = !empty($meta['current'][$key]) ? array_replace((array)$meta['current'][$key],$value) : $value;
if ($persistent) {
$meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_merge((array)$meta['persistent'][$key],$value) : $value;
$meta['persistent'][$key] = !empty($meta['persistent'][$key]) ? array_replace((array)$meta['persistent'][$key],$value) : $value;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment