httpClient example

->setRouteParameter('_format', $format)
      ->setAbsolute();

    $request_body = [];
    if (isset($name)) {
      $request_body['name'] = $name;
    }
    if (isset($pass)) {
      $request_body['pass'] = $pass;
    }

    $result = \Drupal::httpClient()->post($user_login_url->toString()[
      'body' => $this->serializer->encode($request_body$format),
      'headers' => [
        'Accept' => "application/$format",
      ],
      'http_errors' => FALSE,
      'cookies' => $this->cookies,
    ]);
    return $result;
  }

  /** * Tests user session life cycle. */
$this->drupalGet('admin/config/regional/language/add');
    $this->submitForm($this->language, 'Add custom language');
    $this->drupalGet('admin/config/regional/language/detection');
    $this->submitForm(['language_interface[enabled][language-url]' => 1], 'Save settings');
    $this->drupalLogout();

    // Enable access logging.     $this->config('statistics.settings')
      ->set('count_content_views', 1)
      ->save();

    $this->client = \Drupal::httpClient();
  }

  /** * Verifies node hit counter logging and script placement. */
  public function testLogging() {
    $path = 'node/' . $this->node->id();
    $module_path = $this->getModulePath('statistics');
    $stats_path = base_path() . $module_path . '/statistics.php';
    $lib_path = base_path() . $module_path . '/statistics.js';
    $expected_library = '/<script src=".*?' . preg_quote($lib_path, '/.') . '.*?">/is';

    
// Create Basic page node type.     if ($this->profile != 'standard') {
      $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
    }
    $this->privilegedUser = $this->drupalCreateUser([
      'administer statistics',
      'view post access counter',
      'create page content',
    ]);
    $this->drupalLogin($this->privilegedUser);
    $this->testNode = $this->drupalCreateNode(['type' => 'page', 'uid' => $this->privilegedUser->id()]);
    $this->client = \Drupal::httpClient();
  }

  /** * Verifies that the statistics settings page works. */
  public function testStatisticsSettings() {
    $config = $this->config('statistics.settings');
    $this->assertEmpty($config->get('count_content_views'), 'Count content view log is disabled by default.');

    // Enable counter on content view.     $edit['statistics_count_content_views'] = 1;
    
$this->setMockContainerService('state');
    $this->assertNotNull(\Drupal::state());
  }

  /** * Tests the httpClient() method. * * @covers ::httpClient */
  public function testHttpClient() {
    $this->setMockContainerService('http_client');
    $this->assertNotNull(\Drupal::httpClient());
  }

  /** * Tests the entityQuery() method. * * @covers ::entityQuery */
  public function testEntityQuery() {
    $query = $this->createMock(QueryInterface::class);
    $storage = $this->createMock(EntityStorageInterface::class);
    $storage
      
$this->assertEquals($expected$outputnew FormattableMarkup('Statistics token %token replaced.', ['%token' => $input]));
    }

    // Hit the node.     $this->drupalGet('node/' . $node->id());
    // Manually calling statistics.php, simulating ajax behavior.     $nid = $node->id();
    $post = http_build_query(['nid' => $nid]);
    $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
    global $base_url;
    $stats_path = $base_url . '/' . $this->getModulePath('statistics') . '/statistics.php';
    $client = \Drupal::httpClient();
    $client->post($stats_path['headers' => $headers, 'body' => $post]);
    /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
    $statistics = \Drupal::service('statistics.storage.node')->fetchView($node->id());

    // Generate and test tokens.     $tests = [];
    $tests['[node:total-count]'] = 1;
    $tests['[node:day-count]'] = 1;
    $tests['[node:last-view]'] = $date_formatter->format($statistics->getTimestamp());
    $tests['[node:last-view:short]'] = $date_formatter->format($statistics->getTimestamp(), 'short');

    
    $this->container->get('plugin.manager.block')->clearCachedDefinitions();

    // Visit a node to have something show up in the block.     $node = $this->drupalCreateNode(['type' => 'page', 'uid' => $this->blockingUser->id()]);
    $this->drupalGet('node/' . $node->id());
    // Manually calling statistics.php, simulating ajax behavior.     $nid = $node->id();
    $post = http_build_query(['nid' => $nid]);
    $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
    global $base_url;
    $stats_path = $base_url . '/' . $this->getModulePath('statistics') . '/statistics.php';
    $client = \Drupal::httpClient();
    $client->post($stats_path['headers' => $headers, 'body' => $post]);

    // Configure and save the block.     $block = $this->drupalPlaceBlock('statistics_popular_block', [
      'label' => 'Popular content',
      'top_day_num' => 3,
      'top_all_num' => 3,
      'top_last_num' => 3,
    ]);

    // Get some page and check if the block is displayed.
// Not even logging in would make it possible to see the view, because then     // we are denied based on authentication method (cookie).     $this->drupalLogin($this->adminUser);
    $this->drupalGet('test/serialize/auth_with_perm', ['query' => ['_format' => 'json']]);
    $this->assertSession()->statusCodeEquals(403);
    $this->drupalLogout();

    // But if we use the basic auth authentication strategy, we should be able     // to see the page.     $url = $this->buildUrl('test/serialize/auth_with_perm');
    $response = \Drupal::httpClient()->get($url[
      'auth' => [$this->adminUser->getAccountName()$this->adminUser->pass_raw],
      'query' => [
        '_format' => 'json',
      ],
    ]);

    // Ensure that any changes to variables in the other thread are picked up.     $this->refreshVariables();

    $this->assertSession()->statusCodeEquals(200);
  }

  
Home | Imprint | This part of the site doesn't use cookies.