diff --git a/core/modules/node/src/Tests/NodeSaveTest.php b/core/modules/node/src/Tests/NodeSaveTest.php
index b850cb474bbe7281acf79c1bf30af3f57c81f325..c9dc9c0b77f00784ba953ea752e91f31346bc1e5 100644
--- a/core/modules/node/src/Tests/NodeSaveTest.php
+++ b/core/modules/node/src/Tests/NodeSaveTest.php
@@ -49,7 +49,11 @@ protected function setUp() {
    */
   function testImport() {
     // Node ID must be a number that is not in the database.
-    $max_nid = db_query('SELECT MAX(nid) FROM {node}')->fetchField();
+    $nids = \Drupal::entityManager()->getStorage('node')->getQuery()
+      ->sort('nid', 'DESC')
+      ->range(0, 1)
+      ->execute();
+    $max_nid = reset($nids);
     $test_nid = $max_nid + mt_rand(1000, 1000000);
     $title = $this->randomMachineName(8);
     $node = array(