Skip to content

Commit 37c9d12

Browse files
committed
add cronjob, finish tests (#22833)
1 parent 9136564 commit 37c9d12

File tree

8 files changed

+218
-2
lines changed

8 files changed

+218
-2
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\User\Cron;
8+
9+
/**
10+
* Disable expired users.
11+
*/
12+
class DisableExpiredUsers
13+
{
14+
15+
/**
16+
* @var \Magento\User\Model\ResourceModel\User\CollectionFactory
17+
*/
18+
private $collectionFactory;
19+
20+
/**
21+
* @param \Magento\User\Model\ResourceModel\User\CollectionFactory $collectionFactory
22+
*/
23+
public function __construct(
24+
\Magento\User\Model\ResourceModel\User\CollectionFactory $collectionFactory
25+
) {
26+
$this->collectionFactory = $collectionFactory;
27+
}
28+
29+
/**
30+
* Disable all expired user accounts.
31+
*/
32+
public function execute()
33+
{
34+
$users = $this->collectionFactory->create()
35+
->addExpiresAtFilter()
36+
->addFieldToFilter('is_active', 1)
37+
;
38+
/** @var \Magento\User\Model\User $user */
39+
foreach ($users as $user) {
40+
$user->setIsActive(0)
41+
->setExpiresAt(null)
42+
->save();
43+
}
44+
}
45+
}

app/code/Magento/User/Model/ResourceModel/User/Collection.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
*/
1414
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
1515
{
16+
1617
/**
1718
* Define resource model
18-
*
1919
* @return void
2020
*/
2121
protected function _construct()
@@ -41,4 +41,21 @@ protected function _initSelect()
4141
['role_name']
4242
);
4343
}
44+
45+
/**
46+
* Filter users by expires_at.
47+
* @param string|null $now
48+
* @return $this
49+
*/
50+
public function addExpiresAtFilter($now = null)
51+
{
52+
if ($now === null) {
53+
$now = new \DateTime();
54+
$now->format('Y-m-d H:i:s');
55+
}
56+
57+
$this->addFieldToFilter('expires_at', ['lt' => $now]);
58+
59+
return $this;
60+
}
4461
}

app/code/Magento/User/Test/Unit/Model/UserValidationRulesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testAddPasswordConfirmationRule()
4545

4646
public function testAddExpiresAtRule()
4747
{
48-
$this->validator->expects($this->once())->method('addRule')->willReturn($this->validator);
48+
$this->validator->expects($this->atMost(2))->method('addRule')->willReturn($this->validator);
4949
$this->assertSame($this->validator, $this->rules->addExpiresAtRule($this->validator));
5050
}
5151
}

app/code/Magento/User/etc/crontab.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
4+
5+
<group id="default">
6+
<job name="user_expire_users" instance="Magento\User\Cron\DisableExpiredUsers" method="execute">
7+
<schedule>0 * * * *</schedule>
8+
</job>
9+
</group>
10+
11+
</config>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\User\Cron;
8+
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
/**
12+
* @magentoAppArea adminhtml
13+
*/
14+
class DisableExpiredUsersTest extends \PHPUnit\Framework\TestCase
15+
{
16+
/**
17+
* @magentoDataFixture Magento/User/_files/expired_users.php
18+
*/
19+
public function testExecute()
20+
{
21+
/** @var \Magento\User\Cron\DisableExpiredUsers $job */
22+
$job = Bootstrap::getObjectManager()->create(\Magento\User\Cron\DisableExpiredUsers::class);
23+
$job->execute();
24+
25+
/** @var \Magento\User\Model\User $user */
26+
$user = Bootstrap::getObjectManager()->create(\Magento\User\Model\User::class);
27+
$user->loadByUsername('adminUser3');
28+
static::assertEquals(0, $user->getIsActive());
29+
static::assertNull($user->getExpiresAt());
30+
}
31+
32+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\User\Model\ResourceModel\User;
8+
9+
/**
10+
* User collection test
11+
* @magentoAppArea adminhtml
12+
*/
13+
class CollectionTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @var \Magento\User\Model\ResourceModel\User\Collection
17+
*/
18+
protected $collection;
19+
20+
protected function setUp()
21+
{
22+
$this->collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
23+
\Magento\User\Model\ResourceModel\User\Collection::class
24+
);
25+
}
26+
27+
public function testSelectQueryInitialized()
28+
{
29+
static::assertContains(
30+
'main_table.user_id = user_role.user_id AND user_role.parent_id != 0',
31+
$this->collection->getSelect()->__toString()
32+
);
33+
}
34+
35+
/**
36+
* @magentoDataFixture Magento/User/_files/expired_users.php
37+
*/
38+
public function testExpiresAtFilter()
39+
{
40+
$this->collection->addExpiresAtFilter();
41+
static::assertCount(1, $this->collection);
42+
}
43+
}

dev/tests/integration/testsuite/Magento/User/Model/UserTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,17 @@ public function testBeforeSavePasswordTooShort()
387387
$this->_model->save();
388388
}
389389

390+
/**
391+
* @expectedException \Magento\Framework\Exception\LocalizedException
392+
* @expectedExceptionMessage The expiration date must be later than the current date.
393+
* @magentoDbIsolation enabled
394+
*/
395+
public function testBeforeSaveExpireDateBeforeNow()
396+
{
397+
$this->_model->setExpiresAt('2010-01-01 00:00:00');
398+
$this->_model->save();
399+
}
400+
390401
/**
391402
* @dataProvider beforeSavePasswordInsecureDataProvider
392403
* @expectedException \Magento\Framework\Exception\LocalizedException
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
9+
/**
10+
* Create an admin user with expired access date
11+
*/
12+
$userIds = [];
13+
14+
/** @var $model \Magento\User\Model\User */
15+
$model = $objectManager->create(\Magento\User\Model\User::class);
16+
$model->setFirstname("John")
17+
->setLastname("Doe")
18+
->setUsername('adminUser3')
19+
->setPassword(\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD)
20+
->setEmail('[email protected]')
21+
->setRoleType('G')
22+
->setResourceId('Magento_Adminhtml::all')
23+
->setPrivileges("")
24+
->setAssertId(0)
25+
->setRoleId(1)
26+
->setPermission('allow');
27+
$model->save();
28+
$userIds[] = $model->getDataByKey('user_id');
29+
30+
/** @var $model \Magento\User\Model\User */
31+
$futureDate = new \DateTime();
32+
$futureDate->modify('+10 days');
33+
$model = $objectManager->create(\Magento\User\Model\User::class);
34+
$model->setFirstname("John")
35+
->setLastname("Doe")
36+
->setUsername('adminUser4')
37+
->setPassword(\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD)
38+
->setEmail('[email protected]')
39+
->setExpiresAt($futureDate->format('Y-m-d H:i:s'))
40+
->setRoleType('G')
41+
->setResourceId('Magento_Adminhtml::all')
42+
->setPrivileges("")
43+
->setAssertId(0)
44+
->setRoleId(1)
45+
->setPermission('allow');
46+
$model->save();
47+
$userIds[] = $model->getDataByKey('user_id');
48+
49+
// need to bypass model validation to set expired date
50+
$resource = $objectManager->get(\Magento\Framework\App\ResourceConnection::class);
51+
$conn = $resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
52+
$tableName = $resource->getTableName('admin_user');
53+
$sql = "UPDATE " . $tableName . " SET expires_at = '2010-01-01 00:00:00' WHERE user_id=" .
54+
$userIds[0] . ";";
55+
$result = $conn->query($sql);
56+
57+

0 commit comments

Comments
 (0)