ContentEntityHasChangesTest.php in Drupal 10        
                          
                  
                        
  
  
  
  
File
  core/tests/Drupal/KernelTests/Core/Entity/ContentEntityHasChangesTest.php
  
    View source  
  <?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
class ContentEntityHasChangesTest extends KernelTestBase {
  
  protected $bundle = 'test';
  
  protected static $modules = [
    'system',
    'user',
    'entity_test',
  ];
  
  protected function setUp() : void {
    parent::setUp();
    $this
      ->installEntitySchema('user');
    $this
      ->installEntitySchema('entity_test_mulrev_changed_rev');
    $this
      ->installSchema('system', 'sequences');
  }
  
  public function testHasTranslationChanges() {
    $user1 = User::create([
      'name' => 'username1',
      'status' => 1,
    ]);
    $user1
      ->save();
    $user2 = User::create([
      'name' => 'username2',
      'status' => 1,
    ]);
    $user2
      ->save();
    
    $storage = $this->container
      ->get('entity_type.manager')
      ->getStorage('entity_test_mulrev_changed_rev');
    
    $entity = $storage
      ->create([
      'name' => $this
        ->randomString(),
    ]);
    $entity
      ->setRevisionUserId($user1
      ->id());
    $entity
      ->save();
    $this
      ->assertFalse($entity
      ->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes after the entity has been saved.');
    
    $entity_previous_rev_id = $entity
      ->getRevisionId();
    
    $entity
      ->setRevisionCreationTime(time() + 1);
    
    $entity
      ->setRevisionUserId($user2
      ->id());
    
    $entity
      ->setRevisionLogMessage('test');
    
    $entity
      ->setRevisionTranslationAffected(TRUE);
    
    $entity
      ->setChangedTime(time() + 1);
    
    $this
      ->assertFalse($entity
      ->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
    
    $entity
      ->setNewRevision(TRUE);
    $this
      ->assertFalse($entity
      ->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
    
    $entity
      ->save();
    
    $entity = $storage
      ->loadRevision($entity_previous_rev_id);
    $this
      ->assertFalse($entity
      ->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
  }
}