Skip to content
Snippets Groups Projects
Commit 3ffc6e86 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

dev #56994 : user datas discover and transfer moved to library + tests

parent ed05ec93
5 merge requests!2080Sandbox detach zf from storm,!2061Master,!2060Master,!2059Hotline master,!2056Dev#56994 migration des comptes lecteurs chambery
Pipeline #614 failed with stage
in 21 minutes and 13 seconds
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_User_Datas extends Class_Entity {
public function __construct($user) {
$this
->setUser($user)
->setSql(Zend_Registry::get('sql'));
}
public function hasDatas() {
$id = $this->getUser()->getId();
$queries = [['notices_paniers', 'id_user=' . $id . ' and notices != \'\''],
['notices_avis', 'id_user=' . $id],
['cms_avis', 'id_user=' . $id],
['suggestion_achat', 'user_id=' . $id],
['session_formation_inscriptions', 'stagiaire_id=' . $id],
['user_group_memberships', 'user_id=' . $id],
['formulaires', 'id_user=' . $id],
['multimedia_devicehold', 'id_user=' . $id]
];
foreach($queries as $query) {
$result = $this->getSql()
->fetchOne(sprintf('select count(id) from %s where %s',
$query[0], $query[1]));
if (0 < $result)
return true;
}
return false;
}
public function giveTo($params, $for_real) {
if (!$target = $this->_getTargetWith($params))
return false;
if (!$for_real)
return true;
$queries = [['notices_paniers', 'id_user'],
['notices_avis', 'id_user'],
['cms_avis', 'id_user'],
['suggestion_achat', 'user_id'],
['session_formation_inscriptions', 'stagiaire_id'],
['user_group_memberships', 'user_id'],
['formulaires', 'id_user'],
['multimedia_devicehold', 'id_user']
];
$pattern = 'update %s set %s=' . $target->getId() . ' where %s=' . $this->getUser()->getId();
foreach($queries as $query)
$this->getSql()->query(sprintf($pattern, $query[0], $query[1], $query[1]));
return true;
}
public function _getTargetWith($params) {
$targets = Class_Users::findAllBy($params);
if (!$targets) {
$this->setError('No target found');
return;
}
if (1 < count($targets)) {
$this->setError('Too many targets');
return;
}
return current($targets);
}
}
......@@ -15,7 +15,9 @@ echo 'User Data Migration by Stl And Pat since 1854
';
$valid_count = Class_Users::countBy(['statut' => 0, 'role_level' => 2]);
$invalid_count = Class_Users::countBy(['statut' => 1, 'role_level' => 2]);
echo $valid_count . ' valid users in database
' . $invalid_count . ' users marked for deletion
';
......@@ -40,7 +42,7 @@ if (0 == $valid_count) {
$for_real = ('--force' == $argv[1]);
echo $for_real
? 'OH MY, RUNNING FOR REAL, GOOD LUCK'
: 'pro tip: use --force option to run for real, with great powers come great responsibility
: 'pro tip: use --force option to run for real, with great powers comes great responsibility
Simulation mode engaged...';
echo '
......@@ -50,87 +52,32 @@ $without_datas = 0;
$without_target = 0;
$success = 0;
echo date('c') . "\n";
foreach(Class_Users::findAllBy(['statut' => 1, 'role_level' => 2]) as $user) {
if (!user_has_info($user)) {
$datas = new Class_User_Datas($user);
if (!$datas->hasDatas()) {
//$user->delete();
$without_datas++;
continue;
}
if (!$target = get_target_for($user)) {
printf("This %s sucks\n", $user->getLogin());
if (!$datas->giveTo(['login' => 'CHAM' . $user->getLogin()], $for_real)) {
$without_target++;
echo 'F';
continue;
}
$success++;
give_data_to_target($user, $target, $for_real);
echo '.';
//$user->delete();
}
/* foreach(Class_Users::findAllBy(['statut' => 1, 'role_level' => 2]) as $user) { */
/* $write_failed_user_in_csv; */
/* } */
echo "\n" . date('c') . "\n";
printf('
%s without datas
%s without target
%s success
', $without_datas, $without_target, $success);
function user_has_info($user) {
$adapter = Zend_Db_Table::getDefaultAdapter();
$queries = ["select count(id) from notices_paniers where id_user=" . $user->getId() . " and notices != ''",
"select count(id) from notices_avis where id_user=" . $user->getId(),
"select count(id) from cms_avis where id_user=" . $user->getId(),
"select count(id) from suggestion_achat where user_id=" . $user->getId(),
"select count(id) from session_formation_inscriptions where stagiaire_id=" . $user->getId(),
"select count(id) from user_group_memberships where user_id=" . $user->getId(),
"select count(id) from formulaires where id_user=" . $user->getId(),
"select count(id) from multimedia_devicehold where id_user=" . $user->getId(),
];
foreach($queries as $query) {
$result = $adapter->query($query)->fetch();
if (0 < current($result))
return true;
}
return false;
}
function get_target_for($user) {
$target_login = 'CHAM' . $user->getLogin(); // to replace
$targets = Class_Users::findAllBy(['login' => $target_login]);
if (!$targets)
return;
if (1 < count($targets))
return;
return current($targets);
}
function give_data_to_target($from, $to, $for_real) {
if (!$for_real)
return;
$adapter = Zend_Db_Table::getDefaultAdapter();
$queries = ["update notices_paniers set id_user=" . $to->getId() . " where id_user=" . $from->getId() . " and notices != ''",
"update notices_avis set id_user=" . $to->getId() . " where id_user=" . $from->getId(),
"update cms_avis set id_user=" . $to->getId() ." where id_user=" . $from->getId(),
"update suggestion_achat set user_id=" . $to->getId() . " where user_id=" . $from->getId(),
"update session_formation_inscriptions set stagiaire_id=" . $to->getId() . " where stagiaire_id=" . $from->getId(),
"update user_group_memberships set user_id=" . $to->getId() . " where user_id=" . $from->getId(),
"update formulaires set id_user=" . $to->getId() . " where id_user=" . $from->getId(),
"update multimedia_devicehold set id_user=" . $to->getId() . " where id_user=" . $from->getId(),
];
foreach($queries as $query)
$adapter->query($query);
}
\ No newline at end of file
', $without_datas, $without_target, $success);
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class UserDatasTestCase extends ModelTestCase {
protected
$_storm_default_to_volatile = true,
$_sql;
public function setUp() {
parent::setUp();
Zend_Registry::set('sql', $this->_sql = $this->mock());
}
}
class UserDatasTest extends UserDatasTestCase {
protected $_datas;
public function setUp() {
parent::setUp();
$this->fixture('Class_Users', ['id' => 44,
'login' => 'procyon',
'password' => 'prof']);
$this->fixture('Class_Users', ['id' => 45,
'login' => 'actarus',
'password' => 'vega4ever']);
$this->fixture('Class_Users', ['id' => 46,
'login' => 'venusia',
'password' => 'vega4ever']);
$this->_sql->whenCalled('fetchOne')
->answers(0);
$this->_datas = new Class_User_Datas(Class_Users::find(44));
}
/** @test */
public function withoutDatasShouldNotHaveDatas() {
$this->assertFalse($this->_datas->hasDatas());
}
/** @test */
public function withRatingsShouldHaveDatas() {
$this->_sql->whenCalled('fetchOne')
->with('select count(id) from cms_avis where id_user=44')
->answers(35);
$this->assertTrue($this->_datas->hasDatas());
}
/** @test */
public function givingToUnknownShouldDoNothing() {
$this->assertFalse($this->_datas->giveTo(['login' => 'alcor'], true));
$this->assertEquals('No target found', $this->_datas->getError());
}
/** @test */
public function givingToMultipleTargetsShouldDoNothing() {
$this->assertFalse($this->_datas->giveTo(['password' => 'vega4ever'], true));
$this->assertEquals('Too many targets', $this->_datas->getError());
}
/** @test */
public function givingToActarusShouldUpdateDatabase() {
$queries = [['notices_paniers', 'id_user'],
['notices_avis', 'id_user'],
['cms_avis', 'id_user'],
['suggestion_achat', 'user_id'],
['session_formation_inscriptions', 'stagiaire_id'],
['user_group_memberships', 'user_id'],
['formulaires', 'id_user'],
['multimedia_devicehold', 'id_user']
];
foreach($queries as $query)
$this->_sql->whenCalled('query')
->with(sprintf('update %s set %s=45 where %s=44',
$query[0], $query[1], $query[1]))
->answers(1);
$this->assertTrue($this->_datas->giveTo(['login' => 'actarus'], true));
}
/** @test */
public function givingToActarusNotForRealShouldDoNothing() {
$this->assertTrue($this->_datas->giveTo(['login' => 'actarus'], false));
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment