Skip to content

Commit 1723530

Browse files
committed
updates adding tests in and updating readme/composer file
1 parent c302061 commit 1723530

8 files changed

Lines changed: 1335 additions & 22 deletions

File tree

.github/workflows/tests.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [ master, main ]
6+
pull_request:
7+
branches: [ master, main ]
8+
9+
jobs:
10+
tests:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
php-version: ['8.0', '8.1', '8.2', '8.3']
16+
17+
name: PHP ${{ matrix.php-version }}
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
23+
- name: Setup PHP
24+
uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: ${{ matrix.php-version }}
27+
extensions: curl, xml, soap, simplexml, mbstring
28+
coverage: xdebug
29+
30+
- name: Install dependencies
31+
run: composer install --prefer-dist --no-progress --no-interaction
32+
33+
- name: Run tests
34+
run: vendor/bin/phpunit --colors=always
35+
36+
- name: Run tests with coverage
37+
if: matrix.php-version == '8.2'
38+
run: vendor/bin/phpunit --coverage-text --colors=always

README.md

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,80 @@
1-
# Cpanel Webhosting Class
1+
# MyAdmin cPanel Webhosting Plugin
22

3-
Cpanel Webhosting Class
3+
[![Tests](https://github.com/detain/myadmin-cpanel-webhosting/actions/workflows/tests.yml/badge.svg)](https://github.com/detain/myadmin-cpanel-webhosting/actions/workflows/tests.yml)
4+
[![Latest Stable Version](https://poser.pugx.org/detain/myadmin-cpanel-webhosting/version)](https://packagist.org/packages/detain/myadmin-cpanel-webhosting)
5+
[![Total Downloads](https://poser.pugx.org/detain/myadmin-cpanel-webhosting/downloads)](https://packagist.org/packages/detain/myadmin-cpanel-webhosting)
6+
[![License](https://poser.pugx.org/detain/myadmin-cpanel-webhosting/license)](https://packagist.org/packages/detain/myadmin-cpanel-webhosting)
47

5-
## Build Status and Code Analysis
8+
A MyAdmin plugin that provides full cPanel and WHM integration for webhosting management. This package handles account provisioning, suspension, reactivation, termination, DNS zone management, reseller configuration, package management, and SSL certificate operations through the cPanel XML-API.
69

7-
Site | Status
8-
--------------|---------------------------
9-
![Travis-CI](http://i.is.cc/storage/GYd75qN.png "Travis-CI") | [![Build Status](https://travis-ci.org/detain/myadmin-cpanel-webhosting.svg?branch=master)](https://travis-ci.org/detain/myadmin-cpanel-webhosting)
10-
![CodeClimate](http://i.is.cc/storage/GYlageh.png "CodeClimate") | [![Code Climate](https://codeclimate.com/github/detain/myadmin-cpanel-webhosting/badges/gpa.svg)](https://codeclimate.com/github/detain/myadmin-cpanel-webhosting) [![Test Coverage](https://codeclimate.com/github/detain/myadmin-cpanel-webhosting/badges/coverage.svg)](https://codeclimate.com/github/detain/myadmin-cpanel-webhosting/coverage) [![Issue Count](https://codeclimate.com/github/detain/myadmin-cpanel-webhosting/badges/issue_count.svg)](https://codeclimate.com/github/detain/myadmin-cpanel-webhosting)
11-
![Scrutinizer](http://i.is.cc/storage/GYeUnux.png "Scrutinizer") | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/myadmin-plugins/cpanel-webhosting/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/myadmin-plugins/cpanel-webhosting/?branch=master) [![Code Coverage](https://scrutinizer-ci.com/g/myadmin-plugins/cpanel-webhosting/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/myadmin-plugins/cpanel-webhosting/?branch=master) [![Build Status](https://scrutinizer-ci.com/g/myadmin-plugins/cpanel-webhosting/badges/build.png?b=master)](https://scrutinizer-ci.com/g/myadmin-plugins/cpanel-webhosting/build-status/master)
12-
![Codacy](http://i.is.cc/storage/GYi66Cx.png "Codacy") | [![Codacy Badge](https://api.codacy.com/project/badge/Grade/226251fc068f4fd5b4b4ef9a40011d06)](https://www.codacy.com/app/detain/myadmin-cpanel-webhosting) [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/25fa74eb74c947bf969602fcfe87e349)](https://www.codacy.com/app/detain/myadmin-cpanel-webhosting?utm_source=github.com&utm_medium=referral&utm_content=detain/myadmin-cpanel-webhosting&utm_campaign=Badge_Coverage)
13-
![Coveralls](http://i.is.cc/storage/GYjNSim.png "Coveralls") | [![Coverage Status](https://coveralls.io/repos/github/detain/db_abstraction/badge.svg?branch=master)](https://coveralls.io/github/detain/myadmin-cpanel-webhosting?branch=master)
14-
![Packagist](http://i.is.cc/storage/GYacBEX.png "Packagist") | [![Latest Stable Version](https://poser.pugx.org/detain/myadmin-cpanel-webhosting/version)](https://packagist.org/packages/detain/myadmin-cpanel-webhosting) [![Total Downloads](https://poser.pugx.org/detain/myadmin-cpanel-webhosting/downloads)](https://packagist.org/packages/detain/myadmin-cpanel-webhosting) [![Latest Unstable Version](https://poser.pugx.org/detain/myadmin-cpanel-webhosting/v/unstable)](//packagist.org/packages/detain/myadmin-cpanel-webhosting) [![Monthly Downloads](https://poser.pugx.org/detain/myadmin-cpanel-webhosting/d/monthly)](https://packagist.org/packages/detain/myadmin-cpanel-webhosting) [![Daily Downloads](https://poser.pugx.org/detain/myadmin-cpanel-webhosting/d/daily)](https://packagist.org/packages/detain/myadmin-cpanel-webhosting) [![License](https://poser.pugx.org/detain/myadmin-cpanel-webhosting/license)](https://packagist.org/packages/detain/myadmin-cpanel-webhosting)
10+
## Features
1511

12+
- **Account Management** - Create, suspend, unsuspend, and terminate cPanel hosting accounts
13+
- **Reseller Support** - Full reseller account setup with ACL configuration and account limits
14+
- **DNS Management** - Add, edit, and remove DNS zones and records
15+
- **Package Management** - Create, edit, delete, and list hosting packages
16+
- **SSL Certificate Management** - Generate, install, and list SSL certificates
17+
- **Auto-Login** - API endpoint for automatic cPanel login via session tokens
18+
- **XML-API Client** - Complete PHP client for the cPanel/WHM XML-API with curl and fopen support
19+
- **Event-Driven Architecture** - Integrates with Symfony EventDispatcher for modular hook-based operation
1620

1721
## Installation
1822

19-
Install with composer like
23+
Install via Composer:
2024

2125
```sh
2226
composer require detain/myadmin-cpanel-webhosting
2327
```
2428

25-
## License
29+
## Usage
30+
31+
### Plugin Registration
32+
33+
The plugin registers event hooks automatically through the `getHooks()` method:
34+
35+
```php
36+
use Detain\MyAdminCpanel\Plugin;
37+
38+
$hooks = Plugin::getHooks();
39+
// Returns hook mappings for: settings, activate, reactivate,
40+
// deactivate, terminate, api.register, function.requirements, ui.menu
41+
```
2642

27-
The Cpanel Webhosting Class class is licensed under the LGPL-v2.1 license.
43+
### XML-API Client
44+
45+
The included `xmlapi` class provides direct access to the cPanel/WHM XML-API:
46+
47+
```php
48+
$whm = new xmlapi('your-server-ip');
49+
$whm->set_port('2087');
50+
$whm->set_protocol('https');
51+
$whm->set_output('json');
52+
$whm->hash_auth('root', $accessHash);
53+
54+
// List accounts
55+
$accounts = $whm->listaccts();
56+
57+
// Create an account
58+
$whm->createacct([
59+
'username' => 'newuser',
60+
'password' => 'securepass',
61+
'domain' => 'example.com',
62+
]);
63+
```
64+
65+
## Running Tests
66+
67+
```sh
68+
composer install
69+
vendor/bin/phpunit
70+
```
71+
72+
To generate a coverage report:
73+
74+
```sh
75+
vendor/bin/phpunit --coverage-text
76+
```
77+
78+
## License
2879

80+
This package is licensed under the [LGPL-2.1](https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html) license.

composer.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
22
"name": "detain/myadmin-cpanel-webhosting",
33
"type": "myadmin-plugin",
4-
"description": "Cpanel Webhosting Class",
4+
"description": "MyAdmin cPanel Webhosting Plugin - Provides cPanel and WHM integration for the MyAdmin control panel, including account provisioning, DNS management, reseller configuration, and XML-API client functionality.",
55
"keywords": [
66
"cpanel",
7+
"whm",
8+
"webhosting",
79
"administration",
8-
"webhosting"
10+
"xml-api",
11+
"myadmin"
912
],
1013
"license": "LGPL-2.1-only",
1114
"authors": [
@@ -25,16 +28,16 @@
2528
"detain/myadmin-plugin-installer": "dev-master"
2629
},
2730
"require-dev": {
28-
"phpunit/phpunit": "*",
29-
"phpunit/phpunit-mock-objects": "*",
30-
"vlucas/phpdotenv": "*",
31-
"codeclimate/php-test-reporter": "dev-master",
32-
"satooshi/php-coveralls": "*",
33-
"codacy/coverage": "dev-master"
31+
"phpunit/phpunit": "^9.6"
3432
},
3533
"autoload": {
3634
"psr-4": {
3735
"Detain\\MyAdminCpanel\\": "src/"
3836
}
37+
},
38+
"autoload-dev": {
39+
"psr-4": {
40+
"Detain\\MyAdminCpanel\\Tests\\": "tests/"
41+
}
3942
}
4043
}

phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
4+
bootstrap="tests/bootstrap.php"
5+
colors="true"
6+
verbose="true"
7+
failOnRisky="true"
8+
failOnWarning="true"
9+
cacheResultFile=".phpunit.cache/test-results">
10+
<testsuites>
11+
<testsuite name="default">
12+
<directory suffix="Test.php">tests</directory>
13+
</testsuite>
14+
</testsuites>
15+
16+
<coverage cacheDirectory=".phpunit.cache/code-coverage"
17+
processUncoveredFiles="true">
18+
<include>
19+
<directory suffix=".php">src</directory>
20+
</include>
21+
</coverage>
22+
</phpunit>

tests/ApiTest.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
namespace Detain\MyAdminCpanel\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class ApiTest extends TestCase
8+
{
9+
/**
10+
* Tests that the api.php source file exists at the expected path.
11+
* This is a basic sanity check for file presence.
12+
*/
13+
public function testApiFileExists(): void
14+
{
15+
$this->assertFileExists(__DIR__ . '/../src/api.php');
16+
}
17+
18+
/**
19+
* Tests that the api.php file defines the api_auto_cpanel_login function.
20+
* This function provides automatic cPanel login functionality via the API.
21+
*/
22+
public function testApiFileDefinesCpanelLoginFunction(): void
23+
{
24+
$source = file_get_contents(__DIR__ . '/../src/api.php');
25+
$this->assertStringContainsString('function api_auto_cpanel_login', $source);
26+
}
27+
28+
/**
29+
* Tests that api_auto_cpanel_login accepts an $id parameter.
30+
* The function signature should accept a website ID parameter.
31+
*/
32+
public function testApiAutoLoginAcceptsIdParameter(): void
33+
{
34+
$source = file_get_contents(__DIR__ . '/../src/api.php');
35+
$this->assertStringContainsString('function api_auto_cpanel_login($id)', $source);
36+
}
37+
38+
/**
39+
* Tests that the api.php file initializes the return array with error status.
40+
* The default return status should be 'error' to handle failure cases.
41+
*/
42+
public function testApiFileInitializesReturnWithErrorStatus(): void
43+
{
44+
$source = file_get_contents(__DIR__ . '/../src/api.php');
45+
$this->assertStringContainsString("'status' => 'error'", $source);
46+
}
47+
48+
/**
49+
* Tests that the api.php file references the webhosting module.
50+
* The function operates on the webhosting module for cPanel sites.
51+
*/
52+
public function testApiFileReferencesWebhostingModule(): void
53+
{
54+
$source = file_get_contents(__DIR__ . '/../src/api.php');
55+
$this->assertStringContainsString("'webhosting'", $source);
56+
}
57+
58+
/**
59+
* Tests that the api.php file uses JSON decoding for the API response.
60+
* The cPanel API returns JSON that needs to be decoded.
61+
*/
62+
public function testApiFileUsesJsonDecode(): void
63+
{
64+
$source = file_get_contents(__DIR__ . '/../src/api.php');
65+
$this->assertStringContainsString('json_decode', $source);
66+
}
67+
68+
/**
69+
* Tests that the api.php file uses curl for HTTP requests.
70+
* The function uses curl to connect to the cPanel API.
71+
*/
72+
public function testApiFileUsesCurl(): void
73+
{
74+
$source = file_get_contents(__DIR__ . '/../src/api.php');
75+
$this->assertStringContainsString('curl_init', $source);
76+
$this->assertStringContainsString('curl_exec', $source);
77+
$this->assertStringContainsString('curl_setopt', $source);
78+
}
79+
80+
/**
81+
* Tests that the api.php file handles the success case with 'ok' status.
82+
* A successful login should return status 'ok' with the login URL.
83+
*/
84+
public function testApiFileHandlesSuccessCase(): void
85+
{
86+
$source = file_get_contents(__DIR__ . '/../src/api.php');
87+
$this->assertStringContainsString("\$return['status'] = 'ok'", $source);
88+
}
89+
90+
/**
91+
* Tests that the api.php file contains an 'Invalid Website Passed' error message.
92+
* This message is returned when the provided website ID is not found.
93+
*/
94+
public function testApiFileContainsInvalidWebsiteError(): void
95+
{
96+
$source = file_get_contents(__DIR__ . '/../src/api.php');
97+
$this->assertStringContainsString('Invalid Website Passed', $source);
98+
}
99+
100+
/**
101+
* Tests that the api.php file casts the id to integer for safety.
102+
* Preventing SQL injection by casting the ID to int.
103+
*/
104+
public function testApiFileCastsIdToInteger(): void
105+
{
106+
$source = file_get_contents(__DIR__ . '/../src/api.php');
107+
$this->assertStringContainsString('(int)$id', $source);
108+
}
109+
110+
/**
111+
* Tests that the api.php file connects to port 2087 for WHM API.
112+
* Port 2087 is the standard SSL port for the WHM API.
113+
*/
114+
public function testApiFileConnectsToPort2087(): void
115+
{
116+
$source = file_get_contents(__DIR__ . '/../src/api.php');
117+
$this->assertStringContainsString(':2087/', $source);
118+
}
119+
120+
/**
121+
* Tests that the api.php file uses create_user_session API endpoint.
122+
* The cPanel API v1 create_user_session endpoint is used for auto-login.
123+
*/
124+
public function testApiFileUsesCreateUserSessionEndpoint(): void
125+
{
126+
$source = file_get_contents(__DIR__ . '/../src/api.php');
127+
$this->assertStringContainsString('create_user_session', $source);
128+
}
129+
130+
/**
131+
* Tests that the api.php file contains proper PHP opening tag.
132+
* The file should start with <?php for proper PHP parsing.
133+
*/
134+
public function testApiFileStartsWithPhpTag(): void
135+
{
136+
$source = file_get_contents(__DIR__ . '/../src/api.php');
137+
$this->assertStringStartsWith('<?php', $source);
138+
}
139+
140+
/**
141+
* Tests that the api.php file contains error_log for curl failures.
142+
* Curl failures should be logged for debugging purposes.
143+
*/
144+
public function testApiFileLogsErrors(): void
145+
{
146+
$source = file_get_contents(__DIR__ . '/../src/api.php');
147+
$this->assertStringContainsString('error_log', $source);
148+
}
149+
150+
/**
151+
* Tests that the api.php file disables SSL verification.
152+
* Self-signed certificates are common in cPanel environments.
153+
*/
154+
public function testApiFileDisablesSslVerification(): void
155+
{
156+
$source = file_get_contents(__DIR__ . '/../src/api.php');
157+
$this->assertStringContainsString('CURLOPT_SSL_VERIFYPEER', $source);
158+
$this->assertStringContainsString('CURLOPT_SSL_VERIFYHOST', $source);
159+
}
160+
}

0 commit comments

Comments
 (0)