diff --git a/core/modules/filter/src/Entity/FilterFormat.php b/core/modules/filter/src/Entity/FilterFormat.php
index 7419a15..7926b58 100644
--- a/core/modules/filter/src/Entity/FilterFormat.php
+++ b/core/modules/filter/src/Entity/FilterFormat.php
@@ -427,6 +427,22 @@ public function onDependencyRemoval(array $dependencies) {
   /**
    * {@inheritdoc}
    */
+  public function calculateDependencies() {
+    parent::calculateDependencies();
+    // Add user role config entities as dependencies. The permission name is
+    // FALSE when this is a fallback text format and that doesn't depend on any
+    // user role.
+    if ($permission = $this->getPermissionName()) {
+      foreach (user_roles(FALSE, $permission) as $rid => $role) {
+        $this->addDependency($role->getConfigDependencyKey(), $role->getConfigDependencyName());
+      }
+    }
+    return $this->dependencies;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   protected function calculatePluginDependencies(PluginInspectionInterface $instance) {
     // Only add dependencies for plugins that are actually configured. This is
     // necessary because the filter plugin collection will return all available
diff --git a/core/modules/filter/src/Tests/FilterSettingsTest.php b/core/modules/filter/src/Tests/FilterSettingsTest.php
index 9b95336..6511989 100644
--- a/core/modules/filter/src/Tests/FilterSettingsTest.php
+++ b/core/modules/filter/src/Tests/FilterSettingsTest.php
@@ -7,7 +7,10 @@
 
 namespace Drupal\filter\Tests;
 
+use Drupal\Component\Utility\Unicode;
+use Drupal\filter\Entity\FilterFormat;
 use Drupal\simpletest\KernelTestBase;
+use Drupal\user\Entity\Role;
 
 /**
  * Tests filter settings.
@@ -17,11 +20,9 @@
 class FilterSettingsTest extends KernelTestBase {
 
   /**
-   * Modules to enable.
-   *
-   * @var array
+   * {@inheritdoc}
    */
-  public static $modules = array('filter');
+  public static $modules = ['filter', 'user', 'system'];
 
   /**
    * Tests explicit and implicit default settings for filters.
@@ -62,4 +63,81 @@ function testFilterDefaults() {
       )));
     }
   }
+
+  /**
+   * Tests dependencies.
+   */
+  public function testDependencies() {
+    /** @var \Drupal\Core\Config\ConfigFactoryInterface $factory */
+    $factory = $this->container->get('config.factory');
+    $this->installEntitySchema('user');
+
+    // Create a text formats.
+    /** @var \Drupal\filter\FilterFormatInterface $format */
+    $format = FilterFormat::create([
+      'format' => Unicode::strtolower($this->randomMachineName()),
+      'name' => $this->randomString(),
+    ]);
+
+    // Create 2 arbitrary roles allowed to use the text format $format.
+    /** @var \Drupal\user\RoleInterface[] $roles */
+    $roles = [];
+    for ($i = 0; $i < 2; $i++) {
+      $roles[$i] = Role::create([
+        'id' => Unicode::strtolower($this->randomMachineName()),
+        'label' => $this->randomString(),
+        'permissions' => [$format->getPermissionName()],
+      ]);
+      $roles[$i]->save();
+    }
+
+    // Save the format.
+    $format->save();
+
+    // The format should be dependent on both role config entities.
+    $dependencies = $format->getDependencies()['config'];
+    $this->assertIdentical(count($dependencies), 2);
+    $this->assertTrue(in_array($roles[0]->getConfigDependencyName(), $dependencies));
+    $this->assertTrue(in_array($roles[1]->getConfigDependencyName(), $dependencies));
+
+    // Mark this text format as fallback format.
+    $settings = $factory->getEditable('filter.settings');
+    $settings->set('fallback_format', $format->id())->save();
+
+    // Recalculate dependencies by saving the text format.
+    $format->save();
+
+    // The format should not depend anymore on the two roles because now this
+    // text format is the fallback format.
+    $this->assertTrue(empty($format->getDependencies()['config']));
+
+    // Set the fallback format to different format.
+    $settings
+      ->set('fallback_format', Unicode::strtolower($this->randomMachineName()))
+      ->save();
+    $format->save();
+
+    // The format should be again dependent on both role config entities.
+    $dependencies = $format->getDependencies()['config'];
+    $this->assertIdentical(count($dependencies), 2);
+    $this->assertTrue(in_array($roles[0]->getConfigDependencyName(), $dependencies));
+    $this->assertTrue(in_array($roles[1]->getConfigDependencyName(), $dependencies));
+
+    $this->assertTrue(FilterFormat::load($format->id()), 'Filter format exists before deleting the role');
+    // Delete the second role.
+    $roles[1]->delete();
+    // Deleting a role should not delete the filter format.
+    $this->assertTrue(FilterFormat::load($format->id()), 'Filter format exists after deleting the role');
+    $format->save();
+
+    // The format should be dependent only on first role.
+    $dependencies = $format->getDependencies()['config'];
+    $this->assertIdentical(count($dependencies), 1);
+    $this->assertTrue(in_array($roles[0]->getConfigDependencyName(), $dependencies));
+    $this->assertFalse(in_array($roles[1]->getConfigDependencyName(), $dependencies));
+
+    // The text format was not deleted on removal of dependencies.
+    $this->assertFalse(empty($format));
+  }
+
 }
