-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDoBrowser.php
More file actions
112 lines (94 loc) · 2.99 KB
/
DoBrowser.php
File metadata and controls
112 lines (94 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Created by JetBrains PhpStorm.
* User: alex
* Date: 28/08/2013
* Time: 06:24
* To change this template use File | Settings | File Templates.
*/
namespace DrupalPatchUtils;
use Goutte\Client;
use Guzzle\Plugin\Cookie\CookieJar\FileCookieJar;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Form;
class DoBrowser {
/** @var \Goutte\Client */
protected $client;
public function __construct() {
// No point using a cache as d.o emits headers with a max-age=0.
$this->client = new Client();
// Add the cooke plugin from guzzle to ensure that the cookie is stored.
$guzzle_client = $this->getClient()->getClient();
$cookie_plugin = new CookiePlugin(new FileCookieJar($this->ensureCookieFilepath()));
$guzzle_client->addSubscriber($cookie_plugin);
}
/**
* Determines whether the user is already logged in.
*
* @return bool
*/
public function loggedIn() {
$crawler = $this->client->request('GET', 'https://www.drupal.org/user/');
$log_in_button = $crawler->selectButton('Log in');
return $log_in_button->count() == 0;
}
public function login($user, $pass) {
$crawler = $this->client->request('GET', 'https://www.drupal.org/user/');
// Check if already logged in.
if (($select_button = $crawler->selectButton('Log in')) && $select_button->count()) {
$form = $select_button->form();
$crawler = $this->client->submit($form, array('name' => $user, 'pass' => $pass));
$login_errors = $crawler->filter('.messages-error');
if ($login_errors->count() > 0) {
print_r($login_errors);
throw new \Exception("Login to drupal.org failed.");
}
}
return $crawler;
}
public function logout() {
if ($this->loggedIn()) {
$this->client->request('GET', 'https://www.drupal.org/user/logout');
}
}
/**
* @param string $issue_uri
* @return \DrupalPatchUtils\CommentForm
*/
public function getCommentForm($issue_uri) {
$this->client->request('GET', $issue_uri . '/edit');
return new CommentForm($this);
}
public function getIssueForm($project) {
$uri = 'https://www.drupal.org/' . 'node/add/project-issue/' . $project;
$crawler = $this->client->request('GET', $uri);
return new IssueForm($crawler->selectButton('Save')->form());
}
public function getErrors(Crawler $crawler) {
$login_errors = $crawler->filter('.messages.error');
$errors = [];
if ($login_errors->count() > 0) {
foreach ($login_errors as $login_error) {
$errors[] = $login_error->nodeValue;
}
}
return $errors;
}
public function submitForm(Form $form) {
return $this->client->submit(($form));
}
/**
* @return \Goutte\Client
*/
public function getClient() {
return $this->client;
}
protected function ensureCookieFilepath() {
$filepath = sys_get_temp_dir() . '/dopathutils';
if (!file_exists($filepath)) {
touch($filepath);
}
return $filepath;
}
}