summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-08-12 17:34:34 (GMT)
committerAlex Pott2014-08-12 17:34:34 (GMT)
commit2acb1fd6f012159a5a44bda037f2335ab6c12458 (patch)
tree70ce8bb6d4e0c1b5ac1222fda2dfa967d0fb1352
parentf9c1ff235b9cb9936cb056d512653554c2ff1a91 (diff)
Issue #2074255 by cilefen, wiifm, almul0, amunir, Gábor Hojtsy, Jalandhar, JeroenT, vegantriathlete: Add changed time tracking to users.
-rw-r--r--core/modules/user/src/Entity/User.php11
-rw-r--r--core/modules/user/src/Tests/UserCreateTest.php3
-rw-r--r--core/modules/user/src/Tests/UserEditTest.php3
-rw-r--r--core/modules/user/src/Tests/Views/UserChangedTest.php55
-rw-r--r--core/modules/user/src/UserInterface.php3
-rw-r--r--core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_changed.yml46
-rw-r--r--core/modules/user/user.views.inc68
7 files changed, 188 insertions, 1 deletions
diff --git a/core/modules/user/src/Entity/User.php b/core/modules/user/src/Entity/User.php
index 979694b..afaa840 100644
--- a/core/modules/user/src/Entity/User.php
+++ b/core/modules/user/src/Entity/User.php
@@ -431,6 +431,13 @@ class User extends ContentEntityBase implements UserInterface {
/**
* {@inheritdoc}
*/
+ public function getChangedTime() {
+ return $this->get('changed')->value;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['uid'] = BaseFieldDefinition::create('integer')
->setLabel(t('User ID'))
@@ -500,6 +507,10 @@ class User extends ContentEntityBase implements UserInterface {
->setLabel(t('Created'))
->setDescription(t('The time that the user was created.'));
+ $fields['changed'] = BaseFieldDefinition::create('changed')
+ ->setLabel(t('Changed'))
+ ->setDescription(t('The time that the user was last edited.'));
+
$fields['access'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Last access'))
->setDescription(t('The time that the user last accessed the site.'))
diff --git a/core/modules/user/src/Tests/UserCreateTest.php b/core/modules/user/src/Tests/UserCreateTest.php
index 48c0705..4af2b40 100644
--- a/core/modules/user/src/Tests/UserCreateTest.php
+++ b/core/modules/user/src/Tests/UserCreateTest.php
@@ -31,6 +31,9 @@ class UserCreateTest extends WebTestBase {
$user = $this->drupalCreateUser(array('administer users'));
$this->drupalLogin($user);
+ $this->assertEqual($user->getCreatedTime(), REQUEST_TIME, 'Creating a user sets default "created" timestamp.');
+ $this->assertEqual($user->getChangedTime(), REQUEST_TIME, 'Creating a user sets default "changed" timestamp.');
+
// Create a field and an instance.
$field_name = 'test_field';
entity_create('field_storage_config', array(
diff --git a/core/modules/user/src/Tests/UserEditTest.php b/core/modules/user/src/Tests/UserEditTest.php
index a468f4d..4ee1b18 100644
--- a/core/modules/user/src/Tests/UserEditTest.php
+++ b/core/modules/user/src/Tests/UserEditTest.php
@@ -65,6 +65,9 @@ class UserEditTest extends WebTestBase {
$this->drupalPostForm("user/" . $user1->id() . "/edit", $edit, t('Save'));
$this->assertRaw(t("The changes have been saved."));
+ // Make sure the changed timestamp is updated.
+ $this->assertEqual($user1->getChangedTime(), REQUEST_TIME, 'Changing a user sets "changed" timestamp.');
+
// Make sure the user can log in with their new password.
$this->drupalLogout();
$user1->pass_raw = $new_pass;
diff --git a/core/modules/user/src/Tests/Views/UserChangedTest.php b/core/modules/user/src/Tests/Views/UserChangedTest.php
new file mode 100644
index 0000000..290dabf
--- /dev/null
+++ b/core/modules/user/src/Tests/Views/UserChangedTest.php
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\user\Tests\Views\UserChangedTest.
+ */
+
+namespace Drupal\user\Tests\Views;
+
+use Drupal\views\Tests\ViewTestBase;
+use Drupal\views\Tests\ViewTestData;
+
+/**
+ * Tests the changed field.
+ *
+ * @group user
+ */
+class UserChangedTest extends ViewTestBase {
+
+ /**
+ * Modules to enable.
+ *
+ * @var array
+ */
+ public static $modules = array('views_ui', 'user_test_views');
+
+ /**
+ * Views used by this test.
+ *
+ * @var array
+ */
+ public static $testViews = array('test_user_changed');
+
+ protected function setUp() {
+ parent::setUp();
+
+ ViewTestData::createTestViews(get_class($this), array('user_test_views'));
+
+ $this->enableViewsTestModule();
+ }
+
+ /**
+ * Tests changed field.
+ */
+ public function testChangedField() {
+ $path = 'test_user_changed';
+
+ $options = array();
+
+ $this->drupalGet($path, $options);
+
+ $this->assertText(t('Updated date') . ': ' . date('Y-m-d', REQUEST_TIME));
+ }
+
+}
diff --git a/core/modules/user/src/UserInterface.php b/core/modules/user/src/UserInterface.php
index 8b89dd4..e2c44d0 100644
--- a/core/modules/user/src/UserInterface.php
+++ b/core/modules/user/src/UserInterface.php
@@ -7,6 +7,7 @@
namespace Drupal\user;
+use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Session\AccountInterface;
@@ -15,7 +16,7 @@ use Drupal\Core\Session\AccountInterface;
*
* @ingroup user_api
*/
-interface UserInterface extends ContentEntityInterface, AccountInterface {
+interface UserInterface extends ContentEntityInterface, EntityChangedInterface, AccountInterface {
/**
* Whether a user has a certain role.
diff --git a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_changed.yml b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_changed.yml
new file mode 100644
index 0000000..e6ed73e
--- /dev/null
+++ b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_changed.yml
@@ -0,0 +1,46 @@
+base_table: users
+core: '8'
+description: ''
+status: '1'
+display:
+ default:
+ display_options:
+ access:
+ type: none
+ cache:
+ type: none
+ exposed_form:
+ type: basic
+ pager:
+ type: full
+ row:
+ type: fields
+ style:
+ type: default
+ fields:
+ name:
+ id: uid
+ table: users
+ field: uid
+ provider: user
+ changed:
+ id: changed
+ table: users
+ field: changed
+ provider: user
+ date_format: html_date
+ filters: { }
+ display_plugin: default
+ display_title: Master
+ id: default
+ position: 0
+ page_1:
+ display_options:
+ path: test_user_changed
+ display_plugin: page
+ display_title: Page
+ id: page_1
+ position: 0
+label: ''
+id: test_user_changed
+tag: ''
diff --git a/core/modules/user/user.views.inc b/core/modules/user/user.views.inc
index b2e2773..c10892e 100644
--- a/core/modules/user/user.views.inc
+++ b/core/modules/user/user.views.inc
@@ -267,6 +267,74 @@ function user_views_data() {
),
);
+ $data['users']['changed'] = array(
+ 'title' => t('Updated date'),
+ 'help' => t('The date the user was last updated.'),
+ 'field' => array(
+ 'id' => 'date',
+ ),
+ 'sort' => array(
+ 'id' => 'date'
+ ),
+ 'filter' => array(
+ 'id' => 'date',
+ ),
+ );
+
+ $data['users']['changed_fulldate'] = array(
+ 'title' => t('Updated date'),
+ 'help' => t('Date in the form of CCYYMMDD.'),
+ 'argument' => array(
+ 'field' => 'changed',
+ 'id' => 'date_fulldate',
+ ),
+ );
+
+ $data['users']['changed_year_month'] = array(
+ 'title' => t('Updated year + month'),
+ 'help' => t('Date in the form of YYYYMM.'),
+ 'argument' => array(
+ 'field' => 'changed',
+ 'id' => 'date_year_month',
+ ),
+ );
+
+ $data['users']['changed_year'] = array(
+ 'title' => t('Updated year'),
+ 'help' => t('Date in the form of YYYY.'),
+ 'argument' => array(
+ 'field' => 'changed',
+ 'id' => 'date_year',
+ ),
+ );
+
+ $data['users']['changed_month'] = array(
+ 'title' => t('Updated month'),
+ 'help' => t('Date in the form of MM (01 - 12).'),
+ 'argument' => array(
+ 'field' => 'changed',
+ 'id' => 'date_month',
+ ),
+ );
+
+ $data['users']['changed_day'] = array(
+ 'title' => t('Updated day'),
+ 'help' => t('Date in the form of DD (01 - 31).'),
+ 'argument' => array(
+ 'field' => 'changed',
+ 'id' => 'date_day',
+ ),
+ );
+
+ $data['users']['changed_week'] = array(
+ 'title' => t('Updated week'),
+ 'help' => t('Date in the form of WW (01 - 53).'),
+ 'argument' => array(
+ 'field' => 'changed',
+ 'id' => 'date_week',
+ ),
+ );
+
if (\Drupal::moduleHandler()->moduleExists('filter')) {
$data['users']['signature'] = array(
'title' => t('Signature'),