Issues (3627)

Tests/Unit/Helper/IpLookupHelperTest.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2015 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Tests\Unit\Helper;
13
14
use Doctrine\ORM\EntityManager;
15
use Mautic\CoreBundle\Entity\IpAddressRepository;
16
use Mautic\CoreBundle\Helper\CoreParametersHelper;
17
use Mautic\CoreBundle\Helper\IpLookupHelper;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
21
class IpLookupHelperTest extends \PHPUnit\Framework\TestCase
22
{
23
    public function __construct($name = null, array $data = [], $dataName = '')
24
    {
25
        parent::__construct($name, $data, $dataName);
26
    }
27
28
    protected function setUp(): void
29
    {
30
        defined('MAUTIC_ENV') or define('MAUTIC_ENV', 'test');
31
    }
32
33
    /**
34
     * @testdox Check if IP outside a request that local IP is returned
35
     *
36
     * @covers  \Mautic\CoreBundle\Helper\IpLookupHelper::getIpAddress
37
     */
38
    public function testLocalIpIsReturnedWhenNotInRequestScope()
39
    {
40
        $ip = $this->getIpHelper()->getIpAddress();
41
42
        $this->assertEquals('127.0.0.1', $ip->getIpAddress());
43
    }
44
45
    /**
46
     * @testdox Check that the first IP is returned when the request is a proxy
47
     *
48
     * @covers  \Mautic\CoreBundle\Helper\IpLookupHelper::getIpAddress
49
     */
50
    public function testClientIpIsReturnedFromProxy()
51
    {
52
        $request = new Request([], [], [], [], [], ['HTTP_X_FORWARDED_FOR' => '73.77.245.52,10.8.0.2,192.168.0.1']);
53
        $ip      = $this->getIpHelper($request)->getIpAddress();
54
55
        $this->assertEquals('73.77.245.52', $ip->getIpAddress());
56
    }
57
58
    /**
59
     * @testdox Check that the first IP is returned with a web proxy
60
     *
61
     * @covers  \Mautic\CoreBundle\Helper\IpLookupHelper::getIpAddress
62
     */
63
    public function testClientIpIsReturnedFromRequest()
64
    {
65
        $request = new Request([], [], [], [], [], ['REMOTE_ADDR' => '73.77.245.53']);
66
        $ip      = $this->getIpHelper($request)->getIpAddress();
67
68
        $this->assertEquals('73.77.245.53', $ip->getIpAddress());
69
    }
70
71
    /**
72
     * @testdox Check that a local IP is returned for internal IPs
73
     *
74
     * @covers  \Mautic\CoreBundle\Helper\IpLookupHelper::getIpAddress
75
     */
76
    public function testLocalIpIsReturnedForInternalNetworkIp()
77
    {
78
        $request = new Request([], [], [], [], [], ['REMOTE_ADDR' => '192.168.0.1']);
79
        $ip      = $this->getIpHelper($request)->getIpAddress();
80
81
        $this->assertEquals('127.0.0.1', $ip->getIpAddress());
82
    }
83
84
    /**
85
     * @testdox Check that internal IP is returned if track_private_ip_ranges is set to true
86
     *
87
     * @covers  \Mautic\CoreBundle\Helper\IpLookupHelper::getIpAddress
88
     */
89
    public function testInternalNetworkIpIsReturnedIfSetToTrack()
90
    {
91
        $request                  = new Request([], [], [], [], [], ['REMOTE_ADDR' => '192.168.0.1']);
92
        $mockCoreParametersHelper = $this
93
            ->getMockBuilder(CoreParametersHelper::class)
94
            ->disableOriginalConstructor()
95
            ->getMock();
96
        $mockCoreParametersHelper->expects($this->any())
97
            ->method('get')
98
            ->willReturnCallback(
99
                function ($param, $defaultValue) {
100
                    return 'track_private_ip_ranges' === $param ? true : $defaultValue;
101
                }
102
            );
103
        $ip = $this->getIpHelper($request, $mockCoreParametersHelper)->getIpAddress();
104
105
        $this->assertEquals('192.168.0.1', $ip->getIpAddress());
106
    }
107
108
    /**
109
     * @param null $request
110
     * @param null $mockCoreParametersHelper
111
     *
112
     * @return IpLookupHelper
113
     */
114
    private function getIpHelper($request = null, $mockCoreParametersHelper = null)
115
    {
116
        $requestStack = new RequestStack();
117
118
        if ($request) {
119
            $requestStack->push($request);
120
        }
121
122
        $mockRepository = $this
123
            ->getMockBuilder(IpAddressRepository::class)
124
            ->disableOriginalConstructor()
125
            ->getMock();
126
        $mockRepository->expects($this->any())
127
            ->method('__call')
128
            ->with($this->equalTo('findOneByIpAddress'))
129
            ->willReturn(null);
130
131
        $mockEm = $this
132
            ->getMockBuilder(EntityManager::class)
133
            ->disableOriginalConstructor()
134
            ->getMock();
135
        $mockEm->expects($this->any())
136
            ->method('getRepository')
137
            ->will($this->returnValue($mockRepository));
138
139
        if (is_null($mockCoreParametersHelper)) {
0 ignored issues
show
The condition is_null($mockCoreParametersHelper) is always true.
Loading history...
140
            $mockCoreParametersHelper = $this
141
                ->getMockBuilder(CoreParametersHelper::class)
142
                ->disableOriginalConstructor()
143
                ->getMock();
144
            $mockCoreParametersHelper->expects($this->any())
145
                ->method('get')
146
                ->willReturn(null);
147
        }
148
149
        return new IpLookupHelper($requestStack, $mockEm, $mockCoreParametersHelper);
150
    }
151
}
152